Skip to content

Instantly share code, notes, and snippets.

@diegolameira
Created November 20, 2020 23:19
Show Gist options
  • Save diegolameira/52f190bef6752216d835211f25d2145f to your computer and use it in GitHub Desktop.
Save diegolameira/52f190bef6752216d835211f25d2145f to your computer and use it in GitHub Desktop.
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