Skip to content

Instantly share code, notes, and snippets.

@andhikamaheva
Created July 2, 2018 06:12
Show Gist options
  • 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
}
@lolu-sholar
Copy link

Slight adjustment I'd made to the code from @gulgulia17

  • Check if A is not an array
function solution(A) {
    let x = 1

    if (Array.isArray(A)) {
        A = A.filter(x => x >= 1).sort((a, b) => a - b)

        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