Editor

The Missing Number

#39 · Lists · Beginner · 35 XP · about 5 min

A list holds the whole numbers from 0 to n, each exactly once, except for one number that is missing. The numbers can be in any order, and the list has n items (one fewer than the full set).

Write missing_number(nums) that returns the missing number.

Examples

missing_number([3, 0, 1])                    # returns 2
missing_number([0, 1])                       # returns 2
missing_number([9, 6, 4, 2, 3, 5, 7, 0, 1])  # returns 8
missing_number([1])                          # returns 0

In the second example the list has 2 items, so the full set is 0, 1 and 2, and 2 is the one that is not there.

Constraints

  • 0 <= len(nums) <= 10,000
  • Every value is between 0 and len(nums), with no repeats

Note

An empty list means the full set was only 0, so the answer is 0.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.