3005. Count Elements With Maximum Frequency

양성준·2025년 4월 21일

코딩테스트

목록 보기
30/102

문제

https://leetcode.com/problems/count-elements-with-maximum-frequency/description/

풀이

class Solution {
    public int maxFrequencyElements(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();

        int max = 0;
        int answer = 0;

        for(int n : nums) {
            map.put(n, map.getOrDefault(n,0) + 1);
            max = Math.max(map.get(n), max);
        }

        for(int value : map.values()) {
            if(value == max) {
                answer += max;
            }
        }

        return answer;  
    }
}
profile
백엔드 개발자

0개의 댓글