Sum of Unique Elements
Given an array of integers, calculate the sum of elements that appear only once in the array. Return the sum.
[ 1, 2, 2, 3, 4, 4 ]
Explanation. In the array, the elements 1 and 3 appear only once. Their sum is 1 + 3 = 4.
[ 10, 20, 10, 30 ]
Explanation. 20 and 30 appear only once and their sum is 20 + 30 = 50.
[ 7, 7, 7, 7 ]
Explanation. Every element in the array appears more than once. Therefore, the sum of unique elements is 0.
[ 9 ]
Explanation. The only element in the array is 9 which appears once. Thus, the result is 9.
[ 3, 7, 3, 3, 1, 5, 1 ]
Explanation. 7 and 5 appear only once. The sum is 7 + 5 = 12.
Follow-up: How would you optimize your solution if the input constraints were increased substantially?
1. The input array will contain at least one element and at most 100 elements. 2. Each element in the array is an integer ranging from 1 to 100.
- Views
- 2