Every element of nums appears twice except for one, which appears once. Find that single one, in linear time and using only constant extra space.
nums = [4,1,2,1,2]4nums = [2,2,1]1acc = 0 (XOR's "nothing")x: acc ^= x ๐ flips the bits where x has a 1acc: pairs have cancelled, the single number is left โญ"Different โ 1, same โ 0". Each bit column of acc is just the parity (odd/even count) of the 1s seen in that column. Paired numbers add an even count, so only the single number's bits stay odd.
Works for negative numbers too (the sign bit cancels the same way). A HashSet also works but costs O(n) space, and sorting costs O(n log n). Related: 137 (every other number appears 3ร: count bits mod 3) and 260 (two singles: split them by one differing bit).