Sum of Unique Elements
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
Only 1 and 3 appear once, so their sum is 4.
Every number repeats, leaving no unique contributions.
The unique numbers 0, -5, and 9 add up to 4.
Algorithm Flow

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:
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:
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:
Now we go through the map and check each stored count:
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:
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
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;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
