1, 2, 2, 3, 3, 3, 4, 4, 4, 4 - it returned 3 as the longest run... so after not being able to figure it out I found this website. Hope someone can help me!
Expand|Select|Wrap|Line Numbers
- public class Numbers
- {
- /**
- Computes the length of the longest run (sequence of
- adjacent repeated values) in an array.
- @param values an array of integer values
- @return the length of the longest run in values
- */
- public int lengthOfLongestRun(int[] values)
- {
- int max = 0;
- int count = 0;
- for(int i=0; i < values.length - 2; i ++)
- {
- if(values[i] == values[i+1] && (i < values.length - 1))
- {
- count ++;
- }
- else
- {
- count ++;
- }
- if(max < count)
- {
- max = count;
- }
- if(values[i] != values[i+1])
- {
- count = 0;
- }
- }
- return max;
- }
- }