{"id":386,"date":"2020-03-25T11:08:17","date_gmt":"2020-03-25T11:08:17","guid":{"rendered":"https:\/\/danwritescode.com\/?p=386"},"modified":"2020-06-25T11:16:32","modified_gmt":"2020-06-25T11:16:32","slug":"distinct-codility-100-correct-javascript-solution","status":"publish","type":"post","link":"https:\/\/danwritescode.com\/distinct-codility-100-correct-javascript-solution\/","title":{"rendered":"Distinct Codility 100% Correct Javascript Solution"},"content":{"rendered":"\n
Ok, the Distinct Codility challenge is an easy one and it can be solved in a number of ways.<\/p>\n\n\n\n
I went for something fun since we do Javascript, so I put each distinct value in the array in a property of an object. To avoid searching through the object, I just attributed each time with a value of 1.<\/p>\n\n\n\n
Here is the 100% correct solution with complexity O(N) \/ O(N*log(N)) solution in javascript:<\/p>\n\n\n\n
function solution(A) {\n \/\/ write your code in JavaScript (Node.js 8.9.4)\n const count = new Object();\n for (let i = 0; i< A.length; i++){\n count[A[i]] = 1;\n }\n return(Object.keys(count).length)\n\n}<\/code><\/pre>\n\n\n\n