GitHub

https://github.com/Choidongjun0830

Java

[프로그래머스] 최빈값

gogi masidda 2023. 10. 31. 16:45
class Solution {
    public int solution(int[] array) {
        int[] count = new int[1001];
        int answer = 0;
        for(int i : array){
            count[i]++;
        }
        int max = 0;
        int max_num = 0;
        int max_count = 0;
        for(int i = 0; i < count.length; i++){
            if(count[i] > max ){
                max = count[i];
                max_num = i;
                max_count = 1;
            }
            else if(count[i] == max){
                max_count++;
            }
            if(max_count == 1){
                answer = max_num;
            }
            else {
                answer = -1;
            }
        }
        return answer;
    }
}

 

728x90