Created
November 20, 2020 23:19
-
-
Save diegolameira/52f190bef6752216d835211f25d2145f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const missingInteger = (A) => { | |
// remove dups and filter by integers | |
A = Array.from(new Set(A)).filter((i) => i > 0); | |
if (!A.length) return 1; | |
const max = Math.max.apply(Math, A); | |
const items = new Array(max); | |
A.forEach((x) => (items[x - 1] = x)); | |
let idx = 0; | |
while (items[idx]) { | |
idx += 1; | |
} | |
return idx + 1; | |
}; | |
test.each` | |
input | expected | |
${[1, 3, 6, 4, 1, 2]} | ${5} | |
${[1, 2, 3]} | ${4} | |
${[-1, -3]} | ${1} | |
`(`should return $expected when inputs $input`, ({ input, expected }) => { | |
expect(missingInteger2(input)).toEqual(expected); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment