The question goes:
Write a method called mode that returns the most frequently occurring element of an array of integers. Assume that the array has at le
Here, I have coded using single loop. We are getting mode from a[j-1] because localCount was recently updated when j was j-1. Also N is size of the array & counts are initialized to 0.
//After sorting the array
i = 0,j=0;
while(i!=N && j!=N){
if(ar[i] == ar[j]){
localCount++;
j++;
}
else{
i++;
localCount = 0;
}
if(localCount > globalCount){
globalCount = localCount;
mode = ar[j-1];
}
}