Codility MissingInteger Javascript solution (proposal)

This afternoon I try to resolve the demo test of Codility. After thinking a lot how to increase the performance (and searching a little bit), I created this code:

function solution(A) {
    let array = [...Array(1000001).keys()];

    const onlyPositives = A.filter(n => n>0);
    if(onlyPositives.length == 0) return 1

    onlyPositives.forEach(a => {
        if(array[a] != null)
            array[a] = null;
    });
    array[0] = null;

   return array.findIndex(e => e != null);
}

Anyone have another Idea?

Leave a Comment