I have to write methods to find the mean, median, mode, etc given an array of numbers. I've gotten everything else worked out, but mode is giving me a hell of a time and don't know why.
The array is 22, 26, 45, 46, 49, 55, 57, 63, 63, 65, 66, 67, 67, 68, 68, 69, 70, 71, 72, 73, 75, 76, 76, 77, 77, 78, 78, 78, 79, 82, 82, 83, 84, 85, 87, 88, 88, 89, 89, 91, 92, 98, 99
It is returning 22, when the answer should be 78.
Here is my code:
public static int mode(int[] array){
int[] countArray = new int[101];
//counts each number
for(int i = 0; i < array.length; i++){
countArray[array[i]]++;
}// end for loop
int mode = array[0], modeIndex = 0;
//finds which number occurs the most
System.out.println(Arrays.toString(countArray));
for(int i = 1; i < array.length; i++){
if(countArray[i] > mode){
mode = countArray[i];
modeIndex = i;
System.out.println(mode + " " + modeIndex);
}// end if
}// end for loop
return modeIndex;
}// end method mode