Sum of Unique Elements
Given an array of integers, compute the sum of all unique elements in the array. An element of the array is considered unique if it appears exactly once in the array.
[ 4, 5, 7, 5, 4 ]
Explanation. Both 4 and 5 appear more than once, so the sum of unique elements is just 7.
[ 7, 7, 7, 7 ]
Explanation. All elements appear more than once, therefore there are no unique elements to sum, resulting in 0.
[ 1, 2, 3, 4, 5 ]
Explanation. All elements are unique. Thus the sum is 1+2+3+4+5 = 15.
[ -1, -1, 2, 2, 0 ]
Explanation. -1 and 2 are repeated, only 0 is unique but it adds nothing to the sum.
Follow-up: How would you optimize your solution if the input array contains up to 10,000,000 elements?
- The input list will contain at least one element. - The elements of the array are integers that range from -100 to 100.
- Views
- 3