Single Number III

Problem Id: 260 Difficulty: Medium


Intuition

Solution


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

        # Now, xor is a ^ b
        right_different = xor & (-xor)

        a, b = 0, 0
        for i in nums:
            if i & right_different:
                a ^= i
            else:
                b ^= i
        return [a, b]