PermMissingElem 100% Correct Javascript Codility Solution

This is another simple practice run with a O(n) / O(N * log(N)) solution. Just iterate through the array once and fill an empty array of length N+1 with 1s for each element you find. At the end display the element from 0.

function solution(A) {
  let N = A.length;
  let count = new Array(N+1).fill(0);
  for (let i = 0; i< N; i++) {
      count[A[i]-1] = 1;
  }
  //add 1 because the array starts at 1
  return(count.indexOf(0)+1)
}

See it in action here: https://app.codility.com/demo/results/trainingM9UEJP-YPR/

Leave a Reply