27 Mar 2022

Find the Lowest Positive Integer that does Not Exist in Array

Taken from DailyCodingProblem:

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.

You can modify the input array in-place.

This is the answer I came up with:

const solution = (array: Array<number>): number | any => {
  // make copy of array, then sort ascending, then filter for positive integers
  let workingArray = array.slice().sort().filter(i => i >= 0)

  // loop through the array and if the incremented value
  // of each element isn't found
  // then increment and return that element
  for (let item of workingArray) {
    if (workingArray.includes(item + 1) == false) {
      let result = item + 1
      return result


Solution tested in REPL:


Copyright © Paramdeo Singh · All Rights Reserved · Built with Jekyll

This node last updated November 7, 2023 and is permanently morphing...

Paramdeo Singh Guyana

Generalist. Edgerunner. Riding the wave of consciousness in this treacherous mortal sea.

Technology Design Strategy Literature Personal Blogs
Search Site

Results are from Blog, Link Dumps, and #99Problems