Frequency of each character in a String using unordered_map in C++ Last Updated : 26 Oct, 2020 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 Frequency of each character in a String using unordered_map in C++ 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 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 Check whether two strings are anagrams of each other using unordered_map in C++ Write a function to check whether two given strings are an Anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, "abcd" and "dabc" are an Anagram of each other. Approach: Unordered Map can 3 min read Frequency of Characters in Alphabetical Order Given a string s, the task is to print the frequency of each of the characters of s in alphabetical order.Example: Input: s = "aabccccddd" Output: a2b1c4d3 Since it is already in alphabetical order, the frequency of the characters is returned for each character. Input: s = "geeksforgeeks" Output: e4 9 min read Like