1748. Sum of Unique Elements

양성준·2025년 4월 21일

코딩테스트

목록 보기
29/102

문제

https://leetcode.com/problems/sum-of-unique-elements/description/

풀이

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

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

        for(int key : map.keySet()) {
            int value = map.get(key);
            if(value == 1) {
                sum+=key;
            }
        }

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

0개의 댓글