LeetCode’s Single Number is a problem where an array of integers is given and every element appears twice except for one. The goal is to find that single one.
XOR makes this exercise extremely straight forward. The reason being that when you XOR 3 elements, and two of them are repeated then you get the other element. Say 4 ^ 4 ^ 3
then you get 3
. Thus, the code below will give us the single element in the array that is not repeated.