Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1748.html

1748. Sum of Unique Elements

Level

Easy

Description

You are given an integer array nums. The unique elements of an array are the elements that appear exactly once in the array.

Return the sum of all the unique elements of nums.

Example 1:

Input: nums = [1,2,3,2]

Output: 4

Explanation: The unique elements are [1,3], and the sum is 4.

Example 2:

Input: nums = [1,1,1,1,1]

Output: 0

Explanation: There are no unique elements, and the sum is 0.

Example 3:

Input: nums = [1,2,3,4,5]

Output: 15

Explanation: The unique elements are [1,2,3,4,5], and the sum is 15.

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Solution

Use a hash map to store each number’s occurrence in nums. Then loop over the hash map and calculate the sum of the elements that occur only once.

  • class Solution {
        public int sumOfUnique(int[] nums) {
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            for (int num : nums) {
                int count = map.getOrDefault(num, 0) + 1;
                map.put(num, count);
            }
            int sum = 0;
            Set<Integer> set = map.keySet();
            for (int num : set) {
                int count = map.get(num);
                if (count == 1)
                    sum += num;
            }
            return sum;
        }
    }
    
    ############
    
    class Solution {
        public int sumOfUnique(int[] nums) {
            int[] cnt = new int[101];
            for (int x : nums) {
                ++cnt[x];
            }
            int ans = 0;
            for (int x = 0; x < 101; ++x) {
                if (cnt[x] == 1) {
                    ans += x;
                }
            }
            return ans;
        }
    }
    
  • // OJ: https://leetcode.com/problems/sum-of-unique-elements/
    // Time: O(N)
    // Space: O(N)
    class Solution {
    public:
        int sumOfUnique(vector<int>& A) {
            unordered_map<int, int> m;
            for (int n : A) m[n]++;
            int ans = 0;
            for (auto &[n, cnt] : m) {
                if (cnt == 1) ans += n;
            }
            return ans;
        }
    };
    
  • class Solution:
        def sumOfUnique(self, nums: List[int]) -> int:
            counter = Counter(nums)
            return sum(num for num, cnt in counter.items() if cnt == 1)
    
    ############
    
    # 1748. Sum of Unique Elements
    # https://leetcode.com/problems/sum-of-unique-elements/
    
    class Solution:
        def sumOfUnique(self, nums: List[int]) -> int:
            cnt = Counter(nums)
            
            return sum(n for n in cnt if cnt[n] == 1)
    
    
  • func sumOfUnique(nums []int) (ans int) {
    	cnt := [101]int{}
    	for _, x := range nums {
    		cnt[x]++
    	}
    	for x := 0; x < 101; x++ {
    		if cnt[x] == 1 {
    			ans += x
    		}
    	}
    	return
    }
    
  • function sumOfUnique(nums: number[]): number {
        const cnt = new Array(101).fill(0);
        for (const x of nums) {
            ++cnt[x];
        }
        let ans = 0;
        for (let x = 0; x < 101; ++x) {
            if (cnt[x] == 1) {
                ans += x;
            }
        }
        return ans;
    }
    
    
  • impl Solution {
        pub fn sum_of_unique(nums: Vec<i32>) -> i32 {
            let mut cnt = [0; 101];
            for x in nums {
                cnt[x as usize] += 1;
            }
            let mut ans = 0;
            for x in 1..101 {
                if cnt[x] == 1 {
                    ans += x;
                }
            }
            ans as i32
        }
    }
    
  • class Solution {
        /**
         * @param Integer[] $nums
         * @return Integer
         */
        function sumOfUnique($nums) {
            $sum = 0;
            for ($i = 0; $i < count($nums); $i++) {
                $hashtable[$nums[$i]] += 1;
                if ($hashtable[$nums[$i]] == 1) $sum += $nums[$i];
                if ($hashtable[$nums[$i]] == 2) $sum -= $nums[$i];
            }
            return $sum;
        }
    }
    

All Problems

All Solutions