Sum of Unique Elements
easySave
ArrayCountingHash Table
Given an array of integers, return the sum of all the unique elements. An element is considered unique if it appears exactly once in the array.
Example 1
Input
[ 4, 4, 5, 6, 8, 5, 9 ]
Output
23
Explanation. The unique elements are 6, 8, and 9. The sum is 6+8+9=23.
Example 2
Input
[ 1, 1, 1, 1 ]
Output
0
Explanation. There are no unique elements in the array. Therefore, the sum is 0.
Example 3
Input
[ 7, 7, 8, 3, 3 ]
Output
8
Explanation. The only unique element is 8.
Example 4
Input
[ 5 ]
Output
5
Explanation. The array contains only one element, which is unique by default. The sum is 5.
Follow-up: How would you solve this problem if a modification to the input array is allowed? What if it isn't?
Constraints:
The input array contains at least one element. All elements are integers.
- Views
- 3