Created
March 23, 2023 17:11
-
-
Save moritztim/b0da1078cd387be284be29babe8be4fe to your computer and use it in GitHub Desktop.
just a neat solution we found to a challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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