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 if it appears exactly once in the array.
Example 1
Input
[ [ 1, 2, 3, 2 ] ]
Output
4
Explanation. Only the numbers 1 and 3 appear exactly once. Their sum is 4.
Example 2
Input
[ [ 1, 1, 1, 1, 1 ] ]
Output
0
Explanation. No number in this list is unique, so the result is 0.
Example 3
Input
[ [ -1, -2, -3, -1, 2 ] ]
Output
-1
Explanation. The unique elements here are -2, -3, and 2, which sum up to (-2) + (-3) + 2 = -3.
Example 4
Input
[ [ 10 ] ]
Output
10
Explanation. Since there is only one element, and it is unique, the sum is equal to the element itself: 10.
Follow-up: Can the algorithm handle large input sizes efficiently?
Constraints:
1. The length of the array `nums` is in the range [1, 100].\n2. Each element in the array is in the range [-100, 100].
- Views
- 7