최빈값은 주어진 값 중에서 가장 자주 나오는 값을 의미합니다. 정수 배열 array가 매개변수로 주어질 때, 최빈값을 return 하도록 solution 함수를 완성해보세요. 최빈값이 여러 개면 -1을 return 합니다.
class Solution {
public int solution(int[] array) {
int answer = 0;
int mode = findMode(array); //최빈값 수
return mode;
}
private static int findMode(int[] array){
int mode = 0;
int maxCount =0;
int countOfModes = 0; //최빈값의 수를 count
for(int i = 0 ; i < array.length ; i++){
int currentNum = array[i];
int currentCount = 1;
for(int j = i + 1 ; j < array.length ; j++){
if(currentNum == array[j]){
currentCount++;
}
}
if(currentCount > maxCount){
maxCount = currentCount;
mode = currentNum;
countOfModes =1;
} else if(currentCount == maxCount){
countOfModes++;
}
}
if(countOfModes > 1){
mode = -1;
}
return mode;
}
}