Missing number
July 16, 2022
bit-manipulationProblem URL: Missing number
We can XOR the same number to even it out, for example 3^3 or 5^5 will always be 0. That means we can XOR every number form 1 to number of element in our input array and then XOR the input numbers with it. That will even our every number except the missing number.
class Solution:
def missingNumber(self, nums: List[int]) -> int:
missing = 0
for i in range(1, len(nums)+1):
missing ^= i
for num in nums:
missing ^= num
return missing
Time Complexity: O(n)
Space Complexity: O(1)