Skip to main content
All Questions

Given an array 'nums' with n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.

Easy
Unlock detailed company stats for this questionUpgrade

Given an array nums that contains n distinct numbers taken from the range [0, n], return the one number in that range that's missing.

Example 1

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

n is 3, so the range is 0 to 3, and 2 is missing.

Example 2

Input: nums = [0, 1] Output: 2

n is 2, so the range is 0 to 2. The missing number can be n itself.

Constraints

  • n == nums.length
  • 1 <= n <= 10^4
  • 0 <= nums[i] <= n
  • All values in nums are distinct.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.