Missing Number
https://leetcode.com/problems/missing-number/
Solution 1: Indexing
table = [0] * (len(nums) + 1)
for num in nums:
table[num] = 1
return table.index(0)Solution 2: Gauss’s Formula (Summation)
n = len(nums)
return n * (n + 1) // 2 - sum(nums)Solution 3: XOR
missing = len(nums)
for i, num in enumerate(nums):
missing ^= i ^ num
return missingUtilizing the fact that x ^ x = 0, and x ^ 0 = x. The index and number cancels each other. Initialize missing = len(nums), since the index n is not iterated.