14 / 75
E

Missing Number

#14
arraymathbit-manipulation

Find the missing number in array containing n distinct numbers from 0 to n.

Example:

Input:[3,0,1]
Output:2

Common 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