Sum of Unique Elements - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,3,2]
Output: 4
💡 Note: Elements 1 and 3 appear exactly once, so sum = 1 + 3 = 4. Element 2 appears twice so it's not unique.
Example 2 — All Unique
$ Input: nums = [1,2,3,4,5]
Output: 15
💡 Note: All elements appear exactly once, so sum = 1 + 2 + 3 + 4 + 5 = 15
Example 3 — No Unique Elements
$ Input: nums = [1,1,2,2,3,3]
Output: 0
💡 Note: Every element appears exactly twice, so no element is unique. Sum = 0

Constraints

  • 1 ≤ nums.length ≤ 100
  • -100 ≤ nums[i] ≤ 100

Visualization

Tap to expand
Sum of Unique ElementsFind elements that appear exactly once and sum them1232appears 1xappears 2xappears 1xappears 2xUnique elements: 1 and 3Sum = 1 + 3 = 4
Understanding the Visualization
1
Input Array
Array with some duplicate elements
2
Find Unique
Identify elements appearing exactly once
3
Sum Result
Add up all unique elements
Key Takeaway
🎯 Key Insight: Only elements appearing exactly once contribute to the final sum
Asked in
Amazon 35 Google 28
89.2K Views
Medium Frequency
~10 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen