First Missing Positive

Hard Arrays & Hashing

nums is a list of whole numbers in no particular order. It may hold negative numbers, zeros and repeats.

Return the smallest whole number of 1 or more that nums does not hold.

Example 1

Input: nums = [4,-2,1,2,9]
Output: 3

1 and 2 are in the list, and 3 is not. -2 is not 1 or more, and 4 and 9 come after 3.

Example 2

Input: nums = [5,3,2,1,4]
Output: 6

Example 3

Input: nums = [0,-7,8]
Output: 1

Constraints

  • 1 ≤ nums.length ≤ 3 × 10⁵
  • -2³¹ ≤ nums[i] ≤ 2³¹ - 1

Practice this in an AI round

30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.

See the AI round

Watch the explanation

More Arrays & Hashing

All Arrays & Hashing problems