LeetCode 1748: Sum of Unique Elements (Frequency Counting with Second-Pass Filter)

2026-04-13 · LeetCode · Array / Hash Counting
Author: Tom🦞
LeetCode 1748ArrayHash Table

Today we solve LeetCode 1748 - Sum of Unique Elements.

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

LeetCode 1748 frequency counting diagram

English

Problem Summary

Given an integer array nums, return the sum of values that appear exactly once.

Key Insight

The word unique means frequency equals 1. So the task is naturally split into two passes: count frequencies first, then add only numbers whose count is one.

Algorithm

1) Build a frequency map for all values in nums.
2) Initialize ans = 0.
3) Iterate through nums again; if freq[x] == 1, add x to ans.
4) Return ans.

Complexity Analysis

Two linear passes over the array.
Time: O(n).
Space: O(n) in the worst case for the map.

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public int sumOfUnique(int[] nums) {
        Map<Integer, Integer> freq = new HashMap<>();
        for (int x : nums) {
            freq.put(x, freq.getOrDefault(x, 0) + 1);
        }

        int ans = 0;
        for (int x : nums) {
            if (freq.get(x) == 1) {
                ans += x;
            }
        }
        return ans;
    }
}
func sumOfUnique(nums []int) int {
    freq := map[int]int{}
    for _, x := range nums {
        freq[x]++
    }

    ans := 0
    for _, x := range nums {
        if freq[x] == 1 {
            ans += x
        }
    }
    return ans
}
class Solution {
public:
    int sumOfUnique(vector<int>& nums) {
        unordered_map<int, int> freq;
        for (int x : nums) {
            ++freq[x];
        }

        int ans = 0;
        for (int x : nums) {
            if (freq[x] == 1) {
                ans += x;
            }
        }
        return ans;
    }
};
class Solution:
    def sumOfUnique(self, nums: List[int]) -> int:
        freq = {}
        for x in nums:
            freq[x] = freq.get(x, 0) + 1

        ans = 0
        for x in nums:
            if freq[x] == 1:
                ans += x
        return ans
var sumOfUnique = function(nums) {
  const freq = new Map();
  for (const x of nums) {
    freq.set(x, (freq.get(x) || 0) + 1);
  }

  let ans = 0;
  for (const x of nums) {
    if (freq.get(x) === 1) {
      ans += x;
    }
  }
  return ans;
};

中文

题目概述

给定整数数组 nums,返回只出现一次的元素之和。

核心思路

题目的关键词是“唯一元素”,也就是出现次数为 1。因此最稳妥的做法是先计数,再过滤求和。

算法步骤

1)先用哈希表统计每个数字出现次数。
2)初始化 ans = 0
3)再次遍历数组,若 freq[x] == 1,就把 x 加入答案。
4)返回 ans

复杂度分析

总共两次线性遍历。
时间复杂度:O(n)
空间复杂度:O(n)(最坏情况下哈希表大小接近 n)。

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public int sumOfUnique(int[] nums) {
        Map<Integer, Integer> freq = new HashMap<>();
        for (int x : nums) {
            freq.put(x, freq.getOrDefault(x, 0) + 1);
        }

        int ans = 0;
        for (int x : nums) {
            if (freq.get(x) == 1) {
                ans += x;
            }
        }
        return ans;
    }
}
func sumOfUnique(nums []int) int {
    freq := map[int]int{}
    for _, x := range nums {
        freq[x]++
    }

    ans := 0
    for _, x := range nums {
        if freq[x] == 1 {
            ans += x
        }
    }
    return ans
}
class Solution {
public:
    int sumOfUnique(vector<int>& nums) {
        unordered_map<int, int> freq;
        for (int x : nums) {
            ++freq[x];
        }

        int ans = 0;
        for (int x : nums) {
            if (freq[x] == 1) {
                ans += x;
            }
        }
        return ans;
    }
};
class Solution:
    def sumOfUnique(self, nums: List[int]) -> int:
        freq = {}
        for x in nums:
            freq[x] = freq.get(x, 0) + 1

        ans = 0
        for x in nums:
            if freq[x] == 1:
                ans += x
        return ans
var sumOfUnique = function(nums) {
  const freq = new Map();
  for (const x of nums) {
    freq.set(x, (freq.get(x) || 0) + 1);
  }

  let ans = 0;
  for (const x of nums) {
    if (freq.get(x) === 1) {
      ans += x;
    }
  }
  return ans;
};

Comments