'''
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.

Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?
'''

class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        ans = ref = 0
        for i in range(1, len(nums) + 1):
            ans ^= i
        for num in nums:
            ref ^= num
        return ref ^ ans
