Sum of Unique Elements (LeetCode 1748): Solution and Explanation

Easy
Array
Hash Table
Counting

Add up the values that occur exactly once in the array. Count every value first, then sum the ones whose count is 1.

Intuition

Whether a value is unique depends on the whole array, not just on what has been seen so far. A value that looks new at index 2 may repeat at index 9. So the counting has to finish before any decision is made.

Values lie between 1 and 100, so an array of 101 counters is a perfect hash map: count[x] is how many times x appears.

Once the tallies are complete, a second sweep adds each element whose tally is 1. A unique value appears once in nums, so it is added exactly once.

Approach: Count, then sum

  1. Create count, an array of 101 zeros.
  2. For each x in nums, increment count[x].
  3. Set total = 0. For each x in nums, if count[x] == 1, add x to total.
  4. Return total.

Step-by-step example

nums = [1,2,3,2]
  1. Tally pass: count[1] = 1, count[2] = 1 then 2, count[3] = 1.
  2. Sum pass: 1 has count 1, total = 1.
  3. 2 has count 2, skip.
  4. 3 has count 1, total = 4.
  5. The second 2 is skipped again. Return 4.

Code: Python, Java, C++

1class Solution:
2    def sumOfUnique(self, nums: List[int]) -> int:
3        count = [0] * 101
4        for x in nums:
5            count[x] += 1
6        total = 0
7        for x in nums:
8            if count[x] == 1:
9                total += x
10        return total
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Count, then sum O(n)
Single pass with add and undo O(n)
Hash map counter O(n)
Sort and group O(n log n)
Time

O(n)

Two linear passes over nums.

Space

O(1)

A fixed array of 101 counters, independent of n.

Other approaches

Single pass with add and undo (time O(n), space O(1))

Keep counts while scanning. When a value's count becomes 1, add it to the total; when it becomes 2, subtract it back out; ignore later copies. One pass, same O(1) space.

Hash map counter (time O(n), space O(n))

Use Counter or a HashMap when the value range is large or unknown, then sum the keys with count 1.

Sort and group (time O(n log n), space O(1))

Sort the array and add every value that differs from both neighbours.

Common mistakes

  • Deciding uniqueness during the first pass. A value can look unique until a later copy shows up.
  • Summing the distinct values instead of the unique ones. A value seen twice is distinct but not unique.
  • Sizing the count array as 100, which leaves no slot for the value 100.

Frequently asked questions

What if no value is unique?

Nothing is added and the answer is 0, as in [1,1,1,1,1].

Why a fixed array instead of a hash map?

Values are bounded by 100, so 101 counters cover every possible key with direct indexing and no hashing.

Can this be done in one pass?

Yes. Add a value when its count first reaches 1 and subtract it when the count reaches 2. Later copies change nothing.