Sum of Unique Elements
easySave
ArrayCountingHash Table
Given an array of integers, compute the sum of only the unique elements in the array. An integer is unique in the array if it appears exactly once in the array.
Example 1
Input
[ 4, 5, 7, 5, 4 ]
Output
7
Explanation. The elements 4 and 5 appear more than once, so they are not included in the sum. The only unique element is 7.
Example 2
Input
[ 1, 2, 3, 2, 1 ]
Output
3
Explanation. Here, 1 and 2 are not unique. Thus, the sum is just 3.
Example 3
Input
[ 7 ]
Output
7
Explanation. There is only one element in the array which is also unique.
Example 4
Input
[ 3, 1, 3, 3, 1, 5, 6, 7, 8, 100, 5 ]
Output
121
Explanation. Only numbers 6, 7, 8, and 100 are unique, their sum is 121.
Follow-up: Can you solve this problem in linear time complexity?
Constraints:
- The array may contain negative, zero and positive integers.\n- The size of the array will be at least 1 and at most 1000.
- Views
- 1