Print numbers in descending order along with their frequencies | Function to print the elements in descending along with their frequencies ; Sorts the element in decreasing order ; traverse the array elements ; Prints the number and count ; Prints the last step ; Driver Code
function printElements ( a , n ) { a = a . sort ( compare ) ; a = reverse ( a ) ; var cnt = 1 ; for ( var i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] != a [ i + 1 ] ) { document . write ( a [ i ] + " " + cnt + " " + " " ) ; cnt = 1 ; } else cnt += 1 ; } document . write ( a [ n - 1 ] + " " + cnt + " " + " " ) ; } function reverse ( a ) { var i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } function compare ( a , b ) { if ( a < b ) { return - 1 ; } else if ( a > b ) { return 1 ; } else { return 0 ; } } var a = [ 1 , 1 , 1 , 2 , 3 , 4 , 9 , 9 , 10 ] ; var n = a . length ; printElements ( a , n ) ;