Sum of Unique Elements
easySave
ArrayCountingHash Table
Given an array of integers, return the sum of all unique elements in the array. An element is considered unique if it only appears once in the array.
Example 1
Input
[ 4, 5, 2, 5, 4 ]
Output
2
Explanation. The numbers 4 and 5 appear more than once, so the only unique number is 2.
Example 2
Input
[ 1, 2, 3, 4, 5 ]
Output
15
Explanation. All numbers are unique. Thus, the sum is 1+2+3+4+5=15.
Follow-up: How would you optimize your solution if the array is extremely large and you need to minimize memory usage?
Constraints:
All the elements in the input array are integers. The input array will contain at least one element. The values may be positive, negative, or zero.
- Views
- 2