distinct codility 100% correct javascript solution

Distinct Codility 100% Correct Javascript Solution

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!

3 Comments

  1. Alireza November 15, 2020
  2. Ankit Vashishta April 24, 2022

Leave a Reply