Longest sequence

  • 10
  • Locked
If numbers repeat themselves, does that mean that somebody wants it that way? At present, we need repeated numbers to practice completing tasks with lists. Let's create a list of numbers and add 10 numbers from the keyboard. Our task is to display the length of the longest sequence of repeating numbers in a list.
You can't complete this task, because you're not signed in.
Comments(16)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
JeffLevel 22 , Spartanburg
11 March, 23:37
Always create tests for base cases. In The Longest Sequence those cases are a) all numbers are different, and b) all numbers are the same. Then develop a test for c) a sequence of repeating numbers.
IO.ErrorLevel 19 , Bremen
25 January, 22:05
Check result for 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 as well - maybe this helps someone
JeffLevel 22 , Spartanburg
11 March, 23:30
Good advice.
nullLevel 13 , Orlando
22 January, 20:54
Out of bound issue took me 19 trials to get it correct :(
Liliane TopLevel 17 , Amsterdam
14 February, 21:10
Well at least you got it. I'm still struggling for more than a day and still clueless. Cheers.
nullLevel 13 , Orlando
19 February, 21:43
you can do it. Draw out a picture. Ask a question if you need help. I bet other people will help you get thru it.
PritinaikLevel 8 , Mumbai
30 August 2019, 17:30
Whats wrong with my code? the last requirement is not met whereas i am getting the correct answer.
VrhLevel 9 , Zagreb
13 July 2019, 16:59
Loved this assignment. It was cool to get an error message about numbers bigger than 128 and than figuring it out.
Muhammad VahhaajLevel 19 , Rawalpindi
13 July 2019, 14:51
It is strange that if(127==127) evaluates to true but (128==128) evaluates to false. Maybe it has to do something with the reference type. Hint 1: Use 2 counters, first counter will be assigned the longest occurrence and the second counter will be used to get the occurrences for each set. Hint 2: Use equals method of the Integer class do not use the comparing operator since the Reference objects are dealt(so it will produce weird result in case of numbers greater than 127).
Radostin IvanovLevel 8 , Ruse
28 September 2019, 23:02
Helpful. Thank you :)
Jatin MadaanLevel 16 , Mumbai
6 June 2019, 19:48
I have tried all possible testcases and it is giving right answer but after submission its not passing.
RomanLevel 41
7 June 2019, 07:22
If you need help, something isn't right in your code, the server won't accept your solution (even if you are 100% sure that it is correct). Describe your question/issue in the HELP section at codegym.cc/help.
RyanLevel 14 , Ashburn
18 February 2019, 21:05
My favorite one so far, made me realize how much I've learned.
Grigore TurcanLevel 9 , Chisinau
11 January 2019, 12:47
Please help, what is wrong in my code ?
arvindsidLevel 10 , Chennai
26 December 2018, 18:02
i have verified the following code with all possible scenarios and my results are correct but still it fails verification
package com.codegym.task.task08.task0812;
import java.io.*;
import java.util.ArrayList;


/*
Longest sequence

*/
public class Solution {
    public static void main(String[] args) throws IOException {

        ArrayList<Integer> listNum =  new ArrayList<Integer>();
        int[] seqArray = new int[10];
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int matchNum = 0;
        int matchCount = 0;

        for (int i=0;i<10;i++)
        {
            listNum.add(Integer.parseInt(reader.readLine()));
        }

        for(int i=0;i<10;i++)
        {

            matchNum = listNum.get(i);
        //    System.out.println("matchNum:"+ matchNum);
            matchCount =1;
          for(int j=i+1;j<10;j++)
          {
              if (matchNum == listNum.get(j))
              {
                  matchCount++;
              }
          }
        //  System.out.println("matchCount:"+ matchCount);

          seqArray[i] = matchCount;
        }
        // find the longest sequence in the sequence array

            int s = seqArray[0];
            int l = seqArray[0];

      for(int i = 1; i < seqArray.length; i++) {
         if(seqArray[i] > l)l = seqArray[i];
         else if (seqArray[i] < s)s = seqArray[i];
      }

        System.out.println(l);

    }
}
RomanLevel 41
27 December 2018, 06:48
If you need help, something isn't right in your code, the server won't accept your solution (even if you are 100% sure that it is correct). Describe your question/issue in the HELP section at codegym.cc/help.