Skip to content

Instantly share code, notes, and snippets.

@moritztim
Created March 23, 2023 17:11
Show Gist options
  • Save moritztim/b0da1078cd387be284be29babe8be4fe to your computer and use it in GitHub Desktop.
Save moritztim/b0da1078cd387be284be29babe8be4fe to your computer and use it in GitHub Desktop.
just a neat solution we found to a challenge
/**
* Finds the lowest missing positive number in an array of numbers (quite efficiently).
*/
function findLowestMissingPositive(subject: Array<number>): number {
/**
* The iterator, which will be the lowest missing number after the loop.
*/
let x = -1; // start at -1 because we increment it before the first check
while (1) { // 1 = true
if (!subject.includes(++x)) { break; } // break if the next number isn't in the subject
}
return x; // since we started with 0 and ended with up to 1 more than the length of the subject, this will be the lowest missing number
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment