This method is used to pass in an array of number and "count" the run of numbers and return the longest run. I have all of the code figured out but when I tried testing it with an array like :
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!
[CODE=java]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 lengthOfLongest Run(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;
}
}[/CODE]
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!
[CODE=java]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 lengthOfLongest Run(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;
}
}[/CODE]
Comment