Sum of Unique Elements
easySave
ArrayCountingHash Table
Given an array of integers, compute the sum of all the unique elements in the array. An element is considered unique if it appears exactly once in the array.
Example 1
Input
[ 4, 5, 5, 4, 7 ]
Output
7
Explanation. In the list, 4 and 5 appear more than once, whereas 7 appears exactly once. Thus, the sum of unique elements is 7.
Example 2
Input
[ 1, 2, 2, 3, 4 ]
Output
8
Explanation. 1, 3, and 4 are unique elements. Their sum equals 8.
Example 3
Input
[ 7, 7, 7 ]
Output
0
Explanation. All elements appear more than once, so there are no unique elements. The sum is 0.
Follow-up: Can you solve this problem in O(n) time complexity using extra space?
Constraints:
1. The input array will contain at least one element. 2. All elements are integers.
- Views
- 2