Sum of Unique Elements
easySave
ArrayCountingHash Table
Given an array of integers, calculate the sum of the elements that appear only once in the array. Return the sum of these unique elements.
Example 1
Input
[ [ 1, 2, 2, 1, 3 ] ]
Output
3
Explanation. 3 appears only once and its sum is thus 3.
Example 2
Input
[ [ 4, 4, 4, 4 ] ]
Output
0
Explanation. No numbers appear only once; their sum is therefore 0.
Example 3
Input
[ [ 7, 2, 8, 2, 3, 3, 4 ] ]
Output
19
Explanation. The unique elements are 7, 8, and 4. Their sum is 7+8+4=19.
Example 4
Input
[ [ -1, -1, -2, -2, 0, 0, 10 ] ]
Output
10
Explanation. 10 is the only number that appears exactly once. The sum is therefore 10.
Follow-up: Can this problem be solved in linear time and space complexity?
Constraints:
All elements in the array will be integers. The size of the array will be at least 1 and at most 1,000. Each element can be any integer between -10,000 and 10,000.
- Views
- 3