Sum of Unique Elements
easySave
ArrayCountingHash Table
Given an integer array nums, return the sum of all the unique elements of the array. An element is unique in the array if it appears exactly once in the array.
Example 1
Input
[ 1, 2, 2, 3, 4 ]
Output
8
Explanation. The unique elements are 1, 3, and 4. Their sum is 1 + 3 + 4 = 8.
Example 2
Input
[ 7, 7, 7 ]
Output
0
Explanation. There are no unique elements, so the sum is 0.
Example 3
Input
[ 1 ]
Output
1
Explanation. The only element is unique, so the sum is 1.
Example 4
Input
[ 5, 5, 5, 6 ]
Output
6
Explanation. The unique element is 6, so the sum is 6.
Follow-up: What would be the best way to handle very large arrays in terms of memory and processing speed?
Constraints:
1. The array `nums` will contain at least one element. 2. All elements in `nums` are integers.
- Views
- 2