Sum of Unique Elements
easySave
ArrayCountingHash Table
Given an array of integers, compute the sum of all unique elements in the array. An element is considered unique if it does not have any duplicates in the array.
Example 1
Input
[ 4, 5, 5, 4, 7 ]
Output
7
Explanation. Elements 4 and 5 have duplicates. The sum of the unique element (7) is 7.
Example 2
Input
[ 6, 3, 9, 6, 3 ]
Output
9
Explanation. All elements except 9 have duplicates. Thus, the sum is 9.
Example 3
Input
[ 1, 2, 3, 4, 5 ]
Output
15
Explanation. No duplicates are found, hence the sum of all elements is 15.
Example 4
Input
[ -1, -1, 2, 2, 0 ]
Output
0
Explanation. Only 0 is unique, thus the sum is 0.
Follow-up: Can you solve this problem in O(n) time complexity using extra space?
Constraints:
1. The input array can contain positive and negative numbers. 2. The size of the input array will be at least 1.
- Views
- 2