슈콩

COS PRO 1급 JAVA 누가 당선 되나요 본문

카테고리 없음

COS PRO 1급 JAVA 누가 당선 되나요

shukong 2025. 12. 9. 20:09

 

 

 

[소스 코드]

class Main {
    public int[] solution(int N, int[] votes) {
        [[(guide-anchor):(한줄을 수정하시오)]]
        int voteCounter[] = new int[11];
        for (int i = 0; i < votes.length; i++) {
            voteCounter[votes[i]] += 1;
        }
        int maxVal = 0;
        int cnt = 0;
        for (int i = 1; i <= N; i++) {
            if (maxVal < voteCounter[i]) {
                maxVal = voteCounter[i];
                cnt = 1;
            }
            else if(maxVal == voteCounter[i]){
                cnt += 1;
            }
        }
        int answer[] = new int[cnt];
        for (int i = 1, idx = 0; i <= N; i++){
            if (voteCounter[i] == maxVal) {
                answer[idx] = votes[i];
                idx += 1;
            }
        }
        return answer;
    }
}