Skip to content

Instantly share code, notes, and snippets.

@andhikamaheva
Created July 2, 2018 06:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andhikamaheva/1acc7f669a5c0207a33eb14bdf372bc6 to your computer and use it in GitHub Desktop.
Save andhikamaheva/1acc7f669a5c0207a33eb14bdf372bc6 to your computer and use it in GitHub Desktop.
Codility Demo Test Exercise Solution (JavaScript/NodeJS)
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let tmp = []
for (let i = 0; i < A.length; i++) {
if (0 <= A[i]) {
tmp[A[i]] = true
}
}
for (let i = 1; i <= tmp.length; i++) {
if (undefined === tmp[i]) {
return i
}
}
return 1
}
@gulgulia17
Copy link

gulgulia17 commented Aug 4, 2022

With 100% Score

function solution(A) {
    A = A.filter(x => x >= 1).sort((a, b) => a - b)

    let x = 1

    for(let i = 0; i < A.length; i++) {
        if(x < A[i]) {
            return x
        }
        x = A[i] + 1
    }

    return x
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment