Code Logo

Sum of Unique Elements

Published at05 Jan 2026
Easy 155 views
Like3

This problem is pretty straightforward once you spot the rule. In Sum of Unique Elements, you look through the array and add only the numbers that show up exactly one time.

So the main job is not to add everything. You need to notice which values appear once and which ones repeat. If a number shows up more than once, it does not count toward the final total at all.

For example, if the input is nums = [1,2,3,2], the answer is 4. The number 2 appears twice, so it gets ignored, and only 1 and 3 are added. Another example is nums = [4,4,4], which gives 0. Since every value repeats, there is nothing unique to add.

So the task is to identify which values appear exactly once and return the sum of only those values, ignoring every number that appears two or more times.

Example Input & Output

Example 1
Input
nums = [1,2,3,2]
Output
4
Explanation

Only 1 and 3 appear once, so their sum is 4.

Example 2
Input
nums = [4,4,4]
Output
0
Explanation

Every number repeats, leaving no unique contributions.

Example 3
Input
nums = [0,5,-5,5,9]
Output
15
Explanation

The unique numbers 0, -5, and 9 add up to 4.

Algorithm Flow

Recommendation Algorithm Flow for Sum of Unique Elements
Recommendation Algorithm Flow for Sum of Unique Elements

Solution Approach

A good method for this problem is frequency counting with a JavaScript Map. The idea is simple: first record how many times each number appears, then add only the ones that appear exactly once.

The big benefit of this approach is that it stays efficient and easy to reason about. Instead of checking every number against every other number, which would be slower, the Map lets us store counts as we go and solve the problem in O(n) time.

Once we choose that method, the algorithm becomes two small passes. First, count how many times each number appears. Then, add only the numbers whose count is exactly 1.

We start by creating a map to store frequencies:

const freq = new Map();

This freq map will use each number as the key and its count as the value.

Next, we loop through nums and keep updating that count:

for (const num of nums) {
  freq.set(num, (freq.get(num) || 0) + 1);
}

That line is the important part. freq.get(num) || 0 means if the number has not been seen yet, we start from 0. Then we add 1. After this loop finishes, we know exactly how many times every number appeared.

Once the counts are ready, we create a variable for the answer:

let total = 0;

Now we go through the map and check each stored count:

for (const [num, count] of freq) {
  if (count === 1) {
    total += num;
  }
}

If count === 1, that number is unique, so we add it to total. If the count is bigger than 1, we skip it because it appeared more than once.

At the end, we simply return the answer:

return total;

This works well because the first pass gathers the information we need, and the second pass uses it to build the final sum. The time complexity is O(n), and the space complexity is also O(n) in the worst case.

Best Answers

java
import java.util.*;
class Solution {
    public int sum_of_unique(Object nums) {
        if (nums == null) return 0;
        int[] arr;
        if (nums instanceof int[]) {
            arr = (int[]) nums;
        } else if (nums instanceof List) {
             List<?> list = (List<?>) nums;
             arr = new int[list.size()];
             for(int i=0; i<list.size(); i++) arr[i] = (int) list.get(i);
        } else {
            return 0;
        }
        
        Map<Integer, Integer> map = new HashMap<>();
        for (int n : arr) {
            map.put(n, map.getOrDefault(n, 0) + 1);
        }
        int sum = 0;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                sum += entry.getKey();
            }
        }
        return sum;
    }
}