Print characters in decreasing order of frequency | C # 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
using System ; using System . Collections . Generic ; class GFG { static void printChar ( char [ ] arr , int len ) { Dictionary < char , int > occ = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < len ; i ++ ) if ( occ . ContainsKey ( arr [ i ] ) ) { occ [ arr [ i ] ] = occ [ arr [ i ] ] + 1 ; } else { occ . Add ( arr [ i ] , 1 ) ; } int size = occ . Count ; while ( size -- > 0 ) { int currentMax = 0 ; char arg_max = ( char ) 0 ; foreach ( KeyValuePair < char , int > it in occ ) { if ( it . Value > currentMax || ( it . Value == currentMax && it . Key > arg_max ) ) { arg_max = it . Key ; currentMax = it . Value ; } } Console . Write ( arg_max + " ▁ - ▁ " + currentMax + " STRNEWLINE " ) ; occ . Remove ( arg_max ) ; } } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . Length ; printChar ( str . ToCharArray ( ) , len ) ; } }