Find the First Missing Positive Integer

This problem comes from https://www.dailycodingproblem.com/. Please consider subscribing for more challenges like this.

Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.

For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.

Tips

  1. Study the input carefully. Is there anything you can do with the input to make it easier to find the missing integer?
  2. Study the requirements carefully. Note that you're looking for the first positive integer which is why the first example doesn't return a zero.

Program Extension

  1. Can you make the program work with lists that aren't spaced one integer apart, like two or three? Example: [6, 8, -2, 2] or [-4, -1, 5]
  2. What if the input list contained duplicates? How could you account for that?