1394. Find Lucky Integer in an Array

양성준·2025년 4월 21일

코딩테스트

목록 보기
31/102

문제

https://leetcode.com/problems/find-lucky-integer-in-an-array/description/

풀이

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

        int max = 0;

        for(int n : arr) {
            map.put(n, map.getOrDefault(n,0) + 1);
        }

        for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
            int value = entry.getValue();
            int key = entry.getKey();
            if(value == key) {
                max = Math.max(max, key);
            }
        }

        return max == 0 ? -1 : max;
    }
}
profile
백엔드 개발자

0개의 댓글