{"id":343,"date":"2020-02-24T08:31:00","date_gmt":"2020-02-24T08:31:00","guid":{"rendered":"https:\/\/danwritescode.com\/?p=343"},"modified":"2020-06-23T08:48:15","modified_gmt":"2020-06-23T08:48:15","slug":"permmissingelem-100-correct-javascript-codility-solution","status":"publish","type":"post","link":"https:\/\/danwritescode.com\/permmissingelem-100-correct-javascript-codility-solution\/","title":{"rendered":"PermMissingElem 100% Correct Javascript Codility Solution"},"content":{"rendered":"\n
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.<\/p>\n\n\n\n
function solution(A) {\n let N = A.length;\n let count = new Array(N+1).fill(0);\n for (let i = 0; i< N; i++) {\n count[A[i]-1] = 1;\n }\n \/\/add 1 because the array starts at 1\n return(count.indexOf(0)+1)\n}<\/code><\/pre>\n\n\n\n