{"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

The report is here<\/a>.<\/p>\n\n\n\n

And if you have an even better solution, let me know in the comments!<\/p>\n","protected":false},"excerpt":{"rendered":"

Ok, the Distinct Codility challenge is an easy one and it can be solved in a number of ways. 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. Here is the 100% correct solution with complexity O(N) \/ O(N*log(N)) solution in javascript: The report is here. And if you have an even better solution, let me know in the comments!<\/p>\n","protected":false},"author":1,"featured_media":387,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,12,6],"tags":[],"_links":{"self":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts\/386"}],"collection":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/comments?post=386"}],"version-history":[{"count":1,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts\/386\/revisions"}],"predecessor-version":[{"id":539,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts\/386\/revisions\/539"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/media\/387"}],"wp:attachment":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/media?parent=386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/categories?post=386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/tags?post=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}