I have an array:
int[] anArray = new int[6];
What if the array contained 1,4,5,4,4
? How can I get the most matches? In this case 4 is the most frequent number, and there are three of them, so the function should return 3
.
Or if I have 1,2,1,2,3
it should return 2
.
Or if I have 4,0,1,2,3
it should return 1
.
I really can't figure it out. I tried this:
public static int getCorrectMatches(int[] flowers) throws Exception {
int highest = 0;
for(int index = 0; index < 5; index++) {
int count = countMatches(flowers, index);
if(count > highest) {
highest = index;
}
}
return highest;
}
public static int countMatches(int[] array, int match) throws Exception {
int count = 0;
for(; count < array.length && array[count] == match; count++);
return count;
}
Which didn't work. I'd appreciate any help.