Print characters in decreasing order of frequency | Function to print the characters of the given String in decreasing order of their frequencies ; To store the ; Map 's size ; While there are elements in the map ; Finding the maximum value from the map ; Print the character alongwith its frequency ; Delete the maximum value ; Driver code
function printChar ( arr , len ) { let occ = new Map ( ) ; for ( let i = 0 ; i < len ; i ++ ) if ( occ . has ( arr [ i ] ) ) { occ . set ( arr [ i ] , occ . get ( arr [ i ] ) + 1 ) ; } else { occ . set ( arr [ i ] , 1 ) ; } let size = occ . size ; while ( size -- > 0 ) { let currentMax = 0 ; let arg_max = 0 ; for ( let [ key , value ] of occ . entries ( ) ) { if ( value > currentMax || ( value == currentMax && key > arg_max ) ) { arg_max = key ; currentMax = value ; } } document . write ( arg_max + " " + currentMax + " " ) ; occ . delete ( arg_max ) ; } } let str = " " ; let len = str . length ; printChar ( str . split ( " " ) , len ) ;