Created
July 2, 2018 06:12
-
-
Save andhikamaheva/1acc7f669a5c0207a33eb14bdf372bc6 to your computer and use it in GitHub Desktop.
Codility Demo Test Exercise Solution (JavaScript/NodeJS)
This file contains hidden or 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
// 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 | |
} |
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
With 100% Score