LeetCode 136: Single Number

link

If we xor all numbers, the number without a double will remain.

Time: \mathcal{O}(n), space: \mathcal{O}(1).

class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        single = 0
        for x in nums:
            single ^= x
            
        return single

Leave a comment