Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider this data sequence: \"3 11 5 5 5 2 4 6 6 7 3 -8\". Any value that is th

ID: 3639589 • Letter: C

Question

Consider this data sequence: "3 11 5 5 5 2 4 6 6 7 3 -8". Any value that is the same as the immediately preceding value is considered a CONSECUTIVE DUPLICATE. In this example, there are three such consecutive duplicates: the 2nd and 3rd 5s and the second 6. Note that the last 3 is not a consecutive duplicate because it was preceded by a 7. Write some code that uses a loop to read such a sequence of non-negative integers, terminated by a negative number. When the code finishes executing, the number of consecutive duplicates encountered is printed. In this case,3 would be printed.

ASSUME the availability of a variable, stdin , that references a Scanner object associated with standard input.

Explanation / Answer

Please rate...

Program CountConsecutiveDuplicates.java

===================================================

import java.util.Scanner;

class CountConsecutiveDuplicates
{
    public static void main(String args[])
    {
        Scanner stdin=new Scanner(System.in);
        System.out.println("Enter the numbers one by one [negative number to terminate]: ");
        int c=0,b=-1,max=0;
        while(true)
        {
            int a=stdin.nextInt();
            if(a<0)break;
            if(a==b){c++;if(c>max)max=c;}
            else c=0;
            b=a;
        }
        max++;
        System.out.println("Number of consecutive duplicates: "+max);
    }
}

===================================================

Sample output: