Sum of Unique Elements
Given an array of integers, compute the sum of all the elements that appear exactly once in the array. The input array may contain both positive and negative integers.
[ 4, 5, 7, 5, 4, 8 ]
Explanation. Both integers 7 and 8 appear exactly once in the array. Their sum is 7 + 8 = 15.
[ 1, 2, 3, 2 ]
Explanation. Integers 1 and 3 appear exactly once. Their sum is 1 + 3 = 4.
[ -1, -1, -2, 2 ]
Explanation. Integers -2 and 2 appear exactly once, but their sum is -2 + 2 = 0.
[ 10 ]
Explanation. Only one integer 10 appears in the array, and it appears exactly once. The sum, therefore, is 10.
Follow-up: How would you change your solution if the input needs to support an array of up to 10,000 integers?
The input array will contain at least one integer and at most 100 integers. Integer values will be between -1000 and 1000.
- Views
- 4