Grrr, I just can't figure out how to get this to work properly. Basically I have an array of numbers sorted from lowest to highest and I need to count how many of a certain number there are. What I have:
public static void numberCount(int[]a, int n){
int l,j,k;
k=1;
System.out.println("N\t\tCount");
for (l=0; l<n-1; l++){
j=l+1;
if (a[l]==a[j]){
k++;
}
else{
System.out.println(a[l]+"\t\t"+k);
k=1;
}
}
}
It works great, to a point. Problem is that it won't display the last number in the array and its count because a[j] ends up bigger than the array I have and it won't run that part. Any suggestions?
public static void numberCount(int[]a, int n){
int l,j,k;
k=1;
System.out.println("N\t\tCount");
for (l=0; l<n-1; l++){
j=l+1;
if (a[l]==a[j]){
k++;
}
else{
System.out.println(a[l]+"\t\t"+k);
k=1;
}
}
}
It works great, to a point. Problem is that it won't display the last number in the array and its count because a[j] ends up bigger than the array I have and it won't run that part. Any suggestions?