Sum of Unique Elements
easySave
ArrayCountingHash Table
Given an array of integers, compute the sum of all unique elements in the array. Unique elements are those which appear exactly once in the array.
Example 1
Input
[ [ 4, 5, 7, 5, 4, 8 ] ]
Output
15
Explanation. The numbers 7 and 8 are unique in the array and their sum is 15.
Example 2
Input
[ [ 1, 2, 2, 1, 3 ] ]
Output
3
Explanation. The only unique number is 3.
Example 3
Input
[ [ 7, 7, 7, 7 ] ]
Output
0
Explanation. There are no unique numbers in the array.
Example 4
Input
[ [ -2, -2, -5, 2 ] ]
Output
-3
Explanation. The numbers -5 and 2 are unique. Their sum is -3.
Follow-up: What would be the most efficient way to tackle this problem if the range of the input values is very large?
Constraints:
- The array may contain both positive and negative integers.\n- The size of the array will be at least 1 and at most 10,000.
- Views
- 3