Sum of Unique Elements
easySave
ArrayCountingHash Table
Given an array of integers, calculate the sum of all the elements that have only appeared once in the array.
Example 1
Input
[ 4, 5, 7, 5, 4 ]
Output
7
Explanation. The numbers 4 and 5 appear more than once, while 7 appears only once. Thus, the sum is 7.
Example 2
Input
[ 1, 2, 3, 2 ]
Output
4
Explanation. The number 2 that appears twice, the numbers 1 and 3 appear once. The sum of numbers which appear once is 1 + 3 = 4.
Example 3
Input
[ 10, 10, 10 ]
Output
0
Explanation. All numbers appear more than once, thus there is no sum of unique numbers.
Follow-up: How would you modify your solution if the input array is very large?
Constraints:
The input array will not be empty and will contain at least one integer. All array elements are integers.
- Views
- 1