Print characters in decreasing order of frequency | Java implementation of the approach ; 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
import java . util . * ; class GFG { static void printChar ( char [ ] arr , int len ) { HashMap < Character , Integer > occ = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < len ; i ++ ) if ( occ . containsKey ( arr [ i ] ) ) { occ . put ( arr [ i ] , occ . get ( arr [ i ] ) + 1 ) ; } else { occ . put ( arr [ i ] , 1 ) ; } int size = occ . size ( ) ; while ( size -- > 0 ) { int currentMax = 0 ; char arg_max = 0 ; for ( Map . Entry < Character , Integer > it : occ . entrySet ( ) ) { if ( it . getValue ( ) > currentMax || ( it . getValue ( ) == currentMax && it . getKey ( ) > arg_max ) ) { arg_max = it . getKey ( ) ; currentMax = it . getValue ( ) ; } } System . out . print ( arg_max + " ▁ - ▁ " + currentMax + "NEW_LINE"); occ . remove ( arg_max ) ; } } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . length ( ) ; printChar ( str . toCharArray ( ) , len ) ; } }