Skip to content

Latest commit

 

History

History
23 lines (19 loc) · 584 Bytes

Distinct.md

File metadata and controls

23 lines (19 loc) · 584 Bytes

Compute number of distinct values in an array.

Solution (JavaScript)

If we sort the array then we are simply able to step through it and keep a counter of every time a new number is encountered.

Test Score: 100%

function solution(A) {
    A.sort((a,b) => a-b)
    let count = 0
    let previous = null
    A.forEach((e)=> {
        if (previous !== e) {
            count++
            previous = e
        }
    })
    
    return count
}