Sum of Unique Elements
Given an array of integers, return the sum of all the unique elements in the array. An element is considered unique if it appears exactly once in the array.
[ [ 3, 1, 5, 3, 7, 5 ] ]
Explanation. The unique elements are 1 and 7. The sum of these unique numbers is 1 + 7 = 8.
[ [ -1, -1, 2, 4, 2 ] ]
Explanation. The unique element in the list is 4.
[ [ 10 ] ]
Explanation. The only element in the array is unique; hence the sum is 10.
[ [ 8, 1, 2, 1, 8 ] ]
Explanation. The unique element in the list is 2.
Follow-up: What would be the best approach if integers in the given array could only be between 1 and 100? How would this influence time/space complexity?
1. The array may contain both positive and negative integers.\n2. The length of the array is at least 1 and at most 1000.
- Views
- 2