Sum of Unique Elements
Write a function that takes an array of integers and returns the sum of the elements that appear only once in the array. For example, given the array [4, 3, 2, 4, 1, 2, 3], the function should return 1 because only the element 1 appears exactly once.
[ 4, 3, 2, 4, 1, 2, 3 ]
Explanation. Only the number `1` appears exactly once in the array.
[ 7, 8, 9, 7, 9 ]
Explanation. Only the number `8` appears exactly once in the array.
[ 1, 1, 1 ]
Explanation. No number appears only once; hence, the sum is `0`.
[ 10 ]
Explanation. The single number `10` appears exactly once, so the sum is `10`.
Follow-up: How would your solution change if the input array could contain both positive and negative integers?
The input array will contain at least one element and all elements will be integers.
- Views
- 2