Leetcode: Single Number
1 min readDec 27, 2016
Since every number is repeating twice we can use xor to eliminate the duplicates as n xor n = 0.
So after taking xor of all numbers only the number that appears once will remain
O(1) space, O(n) time complexity
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
res = nums[0]
for n in nums[1:]:
res = res ^ n
return res