Count distinct elements in an array | This method returns count of Unique elements ; Creates and empty hashset ; Traverse the input array ; If not present , then put it in hashtable and increment result ; Driver code
function countDistinct ( arr , n ) { let hs = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) { hs . add ( arr [ i ] ) ; } return hs . size ; } let arr = [ 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 ] ; document . write ( countDistinct ( arr , arr . length ) ) ;