136 Single Number
Single Number
Given an array of integers, every element appears twice except for one. Find that single one.
Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Attempt 2: The above is terrible implementation, and certainly not linear.
The trick here is identifying that when we have the same number twice, and xor will reset that particular bit to zero. Anything that xors with zero will return back the original number. Regardless of the combination of bits that come in, the individual bits will become flipped when it repeats.
Last updated