I am sorry if this question is repetitive, but I am trying to find the mode of an array. This method takes in an integer array as an input and returns the integer that is the mode of the array. Here is my code below:
public static int mode(int[] abc) {
int count = 1;
int number = 0;
int finalCount = 0;
int finalNum = 0;
for(int i = 0; i < abc.length; i++) {
int j = 0;
for(int k = i + j; k < abc.length; j++) {
int x = abc[i];
if(x == abc[k]) {
number = x;
count++;
}
}
if(count > finalCount) {
finalCount = count;
finalNum = number;
}else if(count == finalCount && count > 0 && i != abc.length - 1) {
if(finalNum > number) {
finalNum = number;
}
}else if(count == finalCount && count == 0 && i == abc.length - 1) {
for(int l = 0; l < abc.length - 1; l++) {
int m = abc[0];
if(m > abc[l + 1]) {
finalNum = abc[l + 1];
}else {
finalNum = m;
}
}
}
}
return finalNum;
}
Apparently, this method says it needs to return a result of type int. But if I already have finalNum, which is supposed to be the mode, to be returned, then why is it giving me this error?