Last active
July 30, 2019 04:26
-
-
Save crazy4groovy/2f04fbdc26a9855ff4fe9c4c4658d9ac to your computer and use it in GitHub Desktop.
codility test
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
// 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