Frequency of each character in a String using unordered_map in C++ Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given a string str, the task is to find the frequency of each character of a string using an unordered_map in C++ STL. Examples: Input: str = "geeksforgeeks" Output: r 1 e 4 s 2 g 2 k 2 f 1 o 1 Input: str = "programming" Output: n 1 i 1 p 1 o 1 r 2 a 1 g 2 m 2 Approach: Traverse each character of the given string str.Check whether the current character is present in unordered_map or not.If it is present, then update the frequency of the current characters else insert the characters with frequency 1 as shown below: if(M.find(s[i])==M.end()) { M.insert(make_pair{s[i], 1}); } else { M[s[i]]++; } 4. Traverse the unordered_map and print the frequency of each characters stored as a mapped value. Below is the implementation of the above approach: CPP // C++ program for the above approach #include <bits/stdc++.h> using namespace std; void printFrequency(string str) { // Define an unordered_map unordered_map<char, int> M; // Traverse string str check if // current character is present // or not for (int i = 0; str[i]; i++) { // If the current characters // is not found then insert // current characters with // frequency 1 if (M.find(str[i]) == M.end()) { M.insert(make_pair(str[i], 1)); } // Else update the frequency else { M[str[i]]++; } } // Traverse the map to print the // frequency for (auto& it : M) { cout << it.first << ' ' << it.second << '\n'; } } // Driver Code int main() { string str = "geeksforgeeks"; // Function call printFrequency(str); return 0; } Outputr 1 e 4 s 2 g 2 k 2 f 1 o 1 Comment More infoAdvertise with us Next Article Count of substrings of given string with frequency of each character at most K H hrishikeshkonderu Follow Improve Article Tags : C++ STL cpp-unordered_map frequency-counting cpp-strings +1 More Practice Tags : CPPcpp-stringsSTL Similar Reads Count of substrings of given string with frequency of each character at most K Given a string str, the task is to calculate the number of substrings of the given string such that the frequency of each element of the string is almost K. Examples: Input: str = "abab", K = 1Output: 7Explanation: The substrings such that the frequency of each character is atmost 1 are "a", "b", "a 6 min read Calculate the frequency of each word in the given string Given a string str, the task is to find the frequency of each word in a string. Examples: Input: str = "Geeks For Geeks" Output: For 1 Geeks 2 Explanation: For occurs 1 time and Geeks occurs 2 times in the given string str. Input: str = "learning to code is learning to create and innovate" Output: a 6 min read Find frequency of each character with positions in given Array of Strings Given an array, arr[] consisting of N strings where each character of the string is lower case English alphabet, the task is to store and print the occurrence of every distinct character in every string. Examples:Â Input: arr[] = { "geeksforgeeks", "gfg" }Output: Occurrences of: e = [1 2] [1 3] [1 1 7 min read Print the frequency of adjacent repeating characters in given string Given a string str of length N. The task is to print the frequency of adjacent repeating characters. Examples: Input: str = "Hello"Output: l: 2Explanation: Consecutive Repeating Character from the given string is "l" and its frequency is 2. Input: str = "Hellolllee"Output: l: 2 l: 3 e: 2Explanation: 5 min read Print characters in decreasing order of frequency 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 - 1Input: str = "bbcc" O 10 min read Print characters in decreasing order of frequency 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 - 1Input: str = "bbcc" O 10 min read Like