Created
March 29, 2019 20:42
-
-
Save codesnakers/fe47b4c180f8ffee3da4f6782e0493d1 to your computer and use it in GitHub Desktop.
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
/* | |
find smallest positive integer not in array | |
*/ | |
function solution(A) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
var len = A.length; | |
if(len===0) return 1; | |
if(len===1){ | |
if(A[0]===1) return 2; | |
else return 1; | |
} | |
var arr = []; | |
var isSorted = 1; | |
for(var i=0; i< len; i++){ | |
if(A[i]>0) arr[A[i]] = true; | |
if(A[i]===isSorted){ | |
isSorted++; | |
} | |
} | |
if(isSorted === len+1) return isSorted; | |
if(arr.length===0) return 1; | |
if(arr.length===1){ | |
if(A[0]>1) return 1; | |
else return 2; | |
} | |
for(var i=1; i <= arr.length; i++){ | |
if(arr[i] === undefined){ | |
return i; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment