Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Last active July 30, 2019 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazy4groovy/2f04fbdc26a9855ff4fe9c4c4658d9ac to your computer and use it in GitHub Desktop.
Save crazy4groovy/2f04fbdc26a9855ff4fe9c4c4658d9ac to your computer and use it in GitHub Desktop.
codility test
// given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A
function solution(A) {
const sortedUnique = [...new Set([...A])]
.filter((n) => (n > 0))
.sort((a, b) => (a - b))
if (sortedUnique.length === 0) return 1
let invalidIdx = sortedUnique.findIndex((n, i) => n !== (i + 1))
if (invalidIdx === -1) return sortedUnique[sortedUnique.length - 1] + 1
if (invalidIdx === 0) return 1
return sortedUnique[invalidIdx - 1] + 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment