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 1Input: 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]]++; } - Traverse the unordered_map and print the frequency of each characters stored as a mapped value.
Below is the implementation of the above approach:
// 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; } |
r 1 e 4 s 2 g 2 k 2 f 1 o 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:
- Longest sub-string having frequency of each character less than equal to k
- Index of character depending on frequency count in string
- Largest index for each distinct character in given string with frequency K
- Check if the frequency of any character is more than half the length of the string
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Printing frequency of each character just after its consecutive occurrences
- Print the frequency of each character in Alphabetical order
- Check if frequency of each character is equal to its position in English Alphabet
- Minimum addition/removal of characters to be done to make frequency of each character prime
- Map every character of one string to another such that all occurrences are mapped to the same character
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Find the character in first string that is present at minimum index in second string
- Queries to find the last non-repeating character in the sub-string of a given string
- Queries to find the first non-repeating character in the sub-string of a string
- Frequency of smallest character in first sentence less than that of second sentence
- Frequency of a substring in a string
- Sort a string according to the frequency of characters
- Calculate the frequency of each word in the given string
- Remove odd frequency characters from the string
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.

