14 / 75
Find the missing number in array containing n distinct numbers from 0 to n.
Example:
Input:
[3,0,1]Output:
2Common Mistakes:
- Integer overflow with large n (use XOR instead)
- Not considering that array has n elements but range is [0,n]
- Using extra space with HashSet unnecessarily
Notes:
Alternative: XOR all indices and values. Missing number remains. XOR approach avoids potential overflow: xor all i from 0 to n and all nums[i].
💻
Java Solution Hidden
Enable “Show Full Solution” to view the code
Find the missing number in array containing n distinct numbers from 0 to n.
Example:
Input:
[3,0,1]Output:
2Common Mistakes:
- Integer overflow with large n (use XOR instead)
- Not considering that array has n elements but range is [0,n]
- Using extra space with HashSet unnecessarily
Notes:
Alternative: XOR all indices and values. Missing number remains. XOR approach avoids potential overflow: xor all i from 0 to n and all nums[i].
14/75
Missing Number