Given string str, the task is to print the characters in decreasing order of their frequency. If the frequency of two characters is the same then sort them in descending order alphabetically.
Examples:
Input: str = “geeksforgeeks”
Output:
e – 4
s – 2
k – 2
g – 2
r – 1
o – 1
f – 1
Input: str = “bbcc”
Output:
c – 2
b – 2
Approach:
- Use an unordered_map to store the frequencies of all the elements of the given string.
- Find the maximum frequency element from the map, print it with its frequency, and remove it from the map.
- Repeat the previous step while the map is not empty.
Below is the implementation of the above approach:
CPP
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;// Function to print the characters// of the given string in decreasing// order of their frequenciesvoid printChar(string str, int len){ // To store the unordered_map<char, int> occ; for (int i = 0; i < len; i++) occ[str[i]]++; // Map's size int size = occ.size(); unordered_map<char, int>::iterator it; // While there are elements in the map while (size--) { // Finding the maximum value // from the map 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; } } // Print the character // alongwith its frequency cout << arg_max << " - " << currentMax << endl; // Delete the maximum value occ.erase(arg_max); }}// Driver codeint main(){ string str = "geeksforgeeks"; int len = str.length(); printChar(str, len); return 0;} |
Java
// Java implementation of the approachimport java.util.*;class GFG{// Function to print the characters// of the given String in decreasing// order of their frequenciesstatic void printChar(char []arr, int len){ // To store the 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); } // Map's size int size = occ.size(); // While there are elements in the map while (size-- > 0) { // Finding the maximum value // from the map 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(); } } // Print the character // alongwith its frequency System.out.print(arg_max + " - " + currentMax + "\n"); // Delete the maximum value occ.remove(arg_max); }}// Driver codepublic static void main(String[] args){ String str = "geeksforgeeks"; int len = str.length(); printChar(str.toCharArray(), len);}}// This code is contributed by gauravrajput1 |
C#
// C# implementation of the approachusing System;using System.Collections.Generic;class GFG{// Function to print the characters// of the given String in decreasing// order of their frequenciesstatic void printChar(char []arr, int len){ // To store the 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); } // Map's size int size = occ.Count; // While there are elements in the map while (size-- > 0) { // Finding the maximum value // from the map 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; } } // Print the character // alongwith its frequency Console.Write(arg_max + " - " + currentMax + "\n"); // Delete the maximum value occ.Remove(arg_max); }}// Driver codepublic static void Main(String[] args){ String str = "geeksforgeeks"; int len = str.Length; printChar(str.ToCharArray(), len);}}// This code is contributed by Princi Singh |
e - 4 s - 2 k - 2 g - 2 r - 1 o - 1 f - 1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Sorting element of an array by frequency in decreasing order
- Generate a Number in Decreasing order of Frequencies of characters of a given String
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Minimum characters to be replaced to make frequency of all characters same
- Print the frequency of each character in Alphabetical order
- Sort a String in decreasing order of values associated after removal of values smaller than X
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Print common characters of two Strings in alphabetical order
- Print all distinct characters of a string in order (3 Methods)
- Python code to print common characters of two Strings in alphabetical order
- Print characters having odd frequencies in order of occurrence
- Print characters and their frequencies in order of occurrence using a LinkedHashMap in Java
- Print characters having even frequencies in order of occurrence
- Print characters having prime frequencies in order of occurrence
- Print characters and their frequencies in order of occurrence using Binary Tree
- Print characters and their frequencies in order of occurrence
- Check if a string has all characters with same frequency with one variation allowed
- Check if frequency of all characters can become same by one removal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

