Sum of Unique Elements
Given an array of integers, calculate the sum of elements that are unique in the array. An element is considered unique if it appears exactly once in the array. Return the sum of these unique elements.
[ 4, 5, 7, 5, 4 ]
Explanation. The integers 4 and 5 appear more than once in the array, so only the integer 7 is counted in the sum, which is unique.
[ 7, 8, 8, 9, 10, 10, 7 ]
Explanation. Only the integer 9 is unique and appears exactly once.
[ 1, 1, 2, 2, 3, 3, 4, 5 ]
Explanation. The integers 4 and 5 are unique, thus their sum is 4 + 5 = 9.
[ 20 ]
Explanation. There is only one number in the array and it is unique.
Follow-up: Could this algorithm be optimized to run in O(n) time complexity using extra space?
The array will contain at least one element and all elements will be integers.
- Views
- 2