Sum of Unique Elements

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.

30:00

Sum of Unique Elements
easy
Free written solution
Topics
Companies

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.

Example 1:
Input: {"nums":[1,2,3,2]}
Output: 4
Constraints:
  • 1≤nums.length≤1001 \leq \text{nums.length} \leq 100

  • 1≤nums[i]≤1001 \leq \text{nums}[i] \leq 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,2,3,2]}

Two passes: first tally how many times each value appears, then add up only the values whose tally is exactly 1.

pass 1: tally
pass 2: sum the singles

nums

1
2
3
2

count[x] tally (distinct values in order of first sight)

empty

sum starts after the tally

total = 0

Variables
VariableValue
distinct0
DepthFunction Call
Stack empty
0/10