Continue learning
Practice related DSA questions in the same topic.
Every input value occurs twice except one value that occurs once. Return the unpaired value.
Every input value occurs twice except one value that occurs once. Return the unpaired value.
def solve(values, target): result = 0 i = 0 while (i < len(values)): result = (result ^ values[i]) i = (i + 1) return resultToggle the value at position 0 into the running XOR.
Toggle the value at position 0 into the running XOR.
Paired light switches toggle a lamp twice, restoring its original state.
Equal numbers cancel under XOR. Rearranging pairs cannot change the result.
O(n) time · O(1) auxiliary space
Worked example · {"values":[6,2,6,-3,2],"target":0} ⇒ -3
Edge case 1 · {"values":[0],"target":0} ⇒ 0
Edge case 2 · {"values":[-1,0,0],"target":0} ⇒ -1
This cancellation does not isolate a single value when repeated numbers occur three times.
How would you isolate two different unpaired numbers?