Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 currentNum = 0;
- int currentLength = 0;
- int longestLength = 0;
- for(int a : values)
- {
- if(a == currentNum)
- {
- currentLength++;
- }
- else
- {
- currentNum = a;
- if(longestLength < currentLength){longestLength = currentLength;}
- currentLength = 1;
- }
- }
- if(longestLength < currentLength){longestLength = currentLength;}
- return longestLength;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement