Traverse through array elements and count frequencies ( assuming that elements are limited by limit )
for ( let i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; for ( let i = 0 ; i <= limit ; i ++ ) if ( count [ i ] > 0 ) document . write ( i + " " + count [ i ] + " " ) ; }