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:
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const count = new Object();
for (let i = 0; i< A.length; i++){
count[A[i]] = 1;
}
return(Object.keys(count).length)
}
The report is here.
And if you have an even better solution, let me know in the comments!
return (new Set(A)).size;
You are absolutely right 🙂
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let set = new Set(A);
return set.size;
}