📘
Missing Number
EasyArrays & Hashing
Given an array containing n distinct numbers from 0, 1, 2, ..., n, find the one that is missing.
Example:
Input:
3,0,1
Output:
2
Reasoning:
- The input array is
[3, 0, 1], containingn = 3distinct numbers. - We notice that the array should contain numbers from
0ton, so the complete set should be[0, 1, 2, 3]. - Comparing the input array to the complete set, we find that the number 2 is missing.
- The final output is therefore
2.
Constraints:
- 1 <= n <= 10^4
- 0 <= nums[i] <= n
- All numbers are unique
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.