I am trying to write a java method to find the mode in an unsorted integer array. This works fine when there is only one mode within the array. But it gives out a random number when there's no mode in the array.
And also I need to make sure that my method addresses multimodal arrays as well. But I find difficulty in editing the code to return an array of modes.
public static int mode(int[] marksArray){
int maxValue =0;int maxCount = 0;
for (int i = 0; i < marksArray.length; ++i) {
int count = 0;
for (int j = 0; j < marksArray.length; ++j) {
if (marksArray[j] == marksArray[i]) ++count;
}
if (count > maxCount) {
maxCount = count;
maxValue = marksArray[i];
}
}
return maxValue;
}