gumgum's Garden🌼

Find the number that appears once and others twice

Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

def singleNumber(self, nums: List[int]) -> int:
    ans = 0
    for num in nums:
        ans ^= num
    return ans
Note

Xor of any two num gives the difference of bit as 1 and same bit as 0
x⊕x=1x \oplus x = 1 and x⊕0=xx \oplus 0 = x