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
NoteXor of any two num gives the difference of bit as 1 and same bit as 0
and