You are given an array with random integers. All integers occur an even amount of time throughout the array except for one integer. You are able to use ^ (or XOR) to single out which integer occurs an odd number of times in the array
I do not understand what the compiler is doing step-by-step when running through this code.
Looking at addition/subtraction/XOR principles for comparison of two binary numbers.
int[] a = {20,1,-1,2,-2,3,3,20,5,5,1,2,4,20,4,-1,-2};
int xor = 0;
for (int i = 0; i < a.length; i++){
xor ^= a[i];
}
return xor;
After running the above code 20 will be shown as the integer that occurs an odd number of times (3 times as opposed to all other integers occurring 2 times)
If you debug and step through the code step-by-step, for example, you will store 20, then after hitting 1, it will become 21, then after hitting -1, it will become -22. I do not understand what math the compiler is performing at the 21 -> -22 step.