Sum of Unique Elements
easySave
ArrayCountingHash Table
Given an array of integers, find the sum of all the unique elements in the array. An element of the array is considered unique if it does not repeat within the array.
Example 1
Input
[ [ 1, 2, 3, 2 ] ]
Output
4
Explanation. The unique elements are 1 and 3, and their sum is 4.
Example 2
Input
[ [ 1, 1, 1, 1 ] ]
Output
0
Explanation. There are no unique elements, hence the sum is 0.
Example 3
Input
[ [ -1, -1, 2, 2, 3 ] ]
Output
3
Explanation. The unique element is 3.
Example 4
Input
[ [ 10 ] ]
Output
10
Explanation. The only element is 10 and it's unique, so the sum is 10.
Example 5
Input
[ [ 0, 0, 0, 0 ] ]
Output
0
Explanation. There are no unique elements.
Follow-up: How would the solution change if negative numbers are not allowed?
Constraints:
1. The array will contain at least one element and at most 100 elements.\n2. Each element in the array will be an integer between -100 and 100.
- Views
- 2