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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printChar ( string str , int len ) { unordered_map < char , int > occ ; for ( int i = 0 ; i < len ; i ++ ) occ [ str [ i ] ] ++ ; int size = occ . size ( ) ; unordered_map < char , int > :: iterator it ; while ( size -- ) { unsigned currentMax = 0 ; char arg_max ; for ( it = occ . begin ( ) ; it != occ . end ( ) ; ++ it ) { if ( it -> second > currentMax || ( it -> second == currentMax && it -> first > arg_max ) ) { arg_max = it -> first ; currentMax = it -> second ; } } cout << arg_max << " ▁ - ▁ " << currentMax << endl ; occ . erase ( arg_max ) ; } } int main ( ) { string str = " geeksforgeeks " ; int len = str . length ( ) ; printChar ( str , len ) ; return 0 ; }