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 (42)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Andrew Evans
Level 17 , San Jose, Canada
11 February 2022, 18:03
Integer is an object, use .equals() for comparisons. It will work for values -128 to 127 but then fail. Pretty big gotcha
Justin Smith
Level 39 , Greenfield, USA, United States
13 July 2021, 12:21
Although the task requires the use of ArrayList, this can actually be simplified fairly easily without the use of any type of list or array. Just use two variables to track the current streak length and maximum streak length and adjust accordingly as you read numbers from the keyboard. I used ArrayList anyway because I suspect validation will fail if you don't.
Level 16 , Texas, United States
23 May 2021, 16:57
I think the value should be 4. the longest sequence of the repeating numbers is four fours.
Level 15 , Old Town, United States
28 April 2021, 17:42
The code wasn't long, but the logic took a while. The important thing was to figure out what to do with the count, after it counted the sequence of repeating numbers. And of course, figuring out how to not go out of bounds in your loop.
Level 19 , Bordeaux, France
13 April 2021, 09:56
Well, I'm glad I had to try only twice in 20 mins, with a for each loop and somes if/else in
Maxim Raitin
Level 20 , Tomsk, Russian Federation
24 February 2021, 14:45
Omg.. 17 attempts...
Level 22 , Iasi, Romania
7 January 2021, 21:27
tested the code 20 times with 20 different sequences, everytime i got the right result, everytime got the error to be carrefull what happens if the longest sequnce is at the end. and tested 20 more times, again everything looked right and still got the error. it seems to be that the part with "do it exactly as we did it" it happens more often and often. funny part is... i looked at the solution and it was a lot like mine, but... they wanted to be exactly the same i guess...
Level 41
8 January 2021, 06:40
Please post your question with the attached solution in the section Help. Or send me you solution in PM, I'll check it.
4 October 2021, 15:24
Similar thing here. I've tested with a variety of permutations of input - all produce the right result, but I get "Be sure that if non-repeating numbers are entered, then the length of the sequence is 1." It is. I'm looking at it. Skipping this for now, as I'm not sure what the constraint that I don't know about is and it doesn't seem worth it.
kar-fai chow
Level 8 , Hong Kong, Hong Kong
22 November 2020, 12:38
so why does integer >127 matters? what is going on?
kar-fai chow
Level 8 , Hong Kong, Hong Kong
22 November 2020, 12:52
never mind, found it: "The issue is that your two Integer objects are just that, objects. They do not match because you are comparing your two object references, not the values within. Obviously .equals is overridden to provide a value comparison as opposed to an object reference comparison." "The JVM is caching Integer values. Hence the comparison with == only works for numbers between -128 and 127." https://stackoverflow.com/questions/3637936/java-integer-equals-vs
Chandan Thapa
Level 22 , Dubai, United Arab Emirates
19 October 2020, 18:52
took me almost 2.3 hrs,, hardest so far for me on the course..
Roman Grygorczuk
Level 19 , Cracow, Poland
24 September 2020, 10:25
I believe that my way how I did this is not very handy (two arrays, 10 for loops), but it works and passed in 7 attempts