Finding the longest run of duplicate numbers in an ArrayList? (Beginner java)



This is my first time using stack overflow, so I’m so sorry if this is formatted incorrectly in any way. For a comp sci project, I have to do some different things to a 40-item Array List of random numbers.

The task I’m struggling with is this:

Count the longest run of the same number. A run continues only when consecutive numbers have the same value. The repeated number and the length of the run is then printed. (Ex: Longest run is of number: 3, length is: 5.)

If there is more than one run of maximum length, mark the last one. Print the array with the longest run marked in the following fashion: 1 1 1 6 5 4 6 3 2 3 2 (3 3 3 3 3) 1 5 6 3 4 4 4

I genuinely have no idea how to approach this problem. Even just some pseudocode could be helpful; I know that these should probably be 2 different ‘for’ loops, one that detects the run and the other that prints it. I have some code from a friend who completed this using Arrays instead of ArrayLists:

public String longestRun()
    {
        int maxRun=1;
        int currentLen = 1;
        int repeated = x[0];
        for (int i =1; i< 40-1; i++)
        {
            if (x[i] == x[i+1])
                currentLen++;
            else
            {
                if (currentLen >= maxRun)
                {
                    maxRun = currentLen;
                    repeated = x[i-1];
                    startRun = i-maxRun;
                    endRun = i-1;
                }
                currentLen = 1;

            }
        } 
        return "The longest run is " + maxRun + " and the repeated number is " + repeated ;
    }

public String printParenth()
    {
        for(int i = 0; i<40; i++)
        {
            if(i != startRun+1 && i != endRun+1)
            System.out.print(x[i]);
             else if(i == startRun+1)
             System.out.print("(" + x[i]);
             else 
             System.out.print(x[i] + ")");
       
        }
         return "";
    }

I know how to create the ArrayList, convert to string & print, etc, it’s just this one task that I don’t understand. I assume this should be easier with an ArrayList, considering the increased number and utility of ArrayList methods. Thanks so much in advance, I really appreciate it!

Answer

  • Put your numbers in a list or an array.
  • Set max to 1 (you will always have at least 1 number).
  • initialize count to 1.
  • initialize the variable most to the first number in the array
  • also set the variable last to the first number in the array.
  • Now iterate thru the list starting with the second number.
  • If the current number is the same as last, increment count.
  • then see if count > max.
    • if it is
      • set max = count.
      • set most = last
    • if it isn’t
      • set count = 1
      • set last = current number
  • continue in this fashion until all numbers have been checked.

At the end most will contain the number that was repeated and max will contain the length of the repetition.



Source: stackoverflow