How to write a method which takes an array of integers and returns the mode.If there is more than one mode, it should return the first one
So far I have this, it works in most cases, but I don't see why exactly it returns the first occurrence of the mode.
public static int mode(int[] a) {
int temp,temps;
for(int i=0;i<a.length-1;i++) {
temp=countRepititions(a,a[i]);
temps=countRepititions(a,a[i+1]);
if(temp>temps) {
return a[i];
} else if(temps>temp) {
return a[i+1];
}
}
return a[0];
}
7'soccurence and6'soccurrence it found that6'soccurrence is greater and returning the value from there only. And the loop is not running furether.countRepititions? It's important we know how it returns its data.