The Missing Integer problem in the 4th Codility lesson is marked as medium but I think the solution is pretty straight forward.
The complexity of my solution is O(N) or O(N * log(N)).
First, we check which is the max value of the array. If it is below 0 we immediately return 1.
We then fill an array of length max with the number of occurrences of each positive element from the A array.
Lastly, we check for the first appearance of 0 in the array of occurrence and return the value we were looking for.
.function solution(A) {
let max = Math.max.apply(null,A);
if (max < 0) return 1;
let B = new Array(max).fill(0);
for (let i = 0; i < A.length; i++) {
if (A[i] > 0) {
B[A[i] - 1]++;
}
}
let index = B.indexOf(0);
if ((index === -1)) {
return max + 1;
} else {
return index + 1;
}
}
Click here to see the report of this algo.