a ^= b coincides with a = a ^ b , where ^ is the bitwise XOR operator.
0 ^ 0
This is a neat algorithm. Let trace one execution using the list [8, 12, 8] , for example:
0 ^ 8 = 0000 ^ 1000 = 1000 1000 ^ 1100 = 0100 0100 ^ 1000 = 1100 = 12
The word duplicate is incorrect. This algorithm checks for parity. A simple but somewhat incorrect definition is "when everything has a pair." a pair ... of parity.
[2,2,2,3,3,5,5,5,5] will return 2 because everything else has a pair.
[3,4,5] will actually return 2 ( 011^100^101 → 010 ), because this is the xor of all unpaired elements.
Joe frambach
source share