Given a vector vec, the task is to find the frequency of each element of vec using a map.
Examples:
Input: vec = {1, 2, 2, 3, 1, 4, 4, 5}
Output:
1 2
2 2
3 1
4 2
5 1
Explanation:
1 has occured 2 times
2 has occured 2 times
3 has occured 1 times
4 has occured 2 times
5 has occured 1 times
Input: v1 = {6, 7, 8, 6, 4, 1}
Output:
1 1
4 1
6 2
7 1
8 1
Explanation:
1 has occured 1 times
4 has occured 1 times
6 has occured 2 times
7 has occured 1 times
8 has occured 1 times
Approach:
We can find the frequency of elements in a vector using given four steps efficiently:
- Traverse the elements of the given vector vec.
- check whether the current element is present in map or not.
- If it is present, then update the frequency of the current element, else insert the element with frequency 1 as shown below:
- Traverse the map and print the frequency of each element stored as a mapped value.
if(M.find(vec[i])==M.end()) {
M[vec[i]]=1;
}
else {
M[vec[i]]++;
}
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h> using namespace std; void printFrequency(vector<int> vec) { // Define an map map<int, int> M; // Traverse vector vec check if // current element is present // or not for (int i = 0; vec[i]; i++) { // If the current element // is not found then insert // current element with // frequency 1 if (M.find(vec[i]) == M.end()) { M[vec[i]] = 1; } // Else update the frequency else { M[vec[i]]++; } } // Traverse the map to print the // frequency for (auto& it : M) { cout << it.first << ' ' << it.second << '\n'; } } // Driver Code int main() { vector<int> vec = { 1, 2, 2, 3, 1, 4, 4, 5 }; // Function call printFrequency(vec); return 0; } |
1 2 2 2 3 1 4 2 5 1
Complexity Analysis:
Time Complexity: O(n)
For a given vector of size n, we are iterating over it once. So the time complexity is O(n)
Space Complexity: O(n)
For a given vector of size n, we are using an extra map which can have maximum of n key-values, so space complexity is O(n)
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:
- How to find the maximum element of a Vector using STL in C++?
- How to find index of a given element in a Vector in C++
- How to find the minimum and maximum element of a Vector using STL in C++?
- Last element of vector in C++ (Accessing and updating)
- Maximum element in an array which is equal to its frequency
- Program to create Custom Vector Class in C++
- How to find the sum of elements of a Vector using STL in C++?
- How to find common elements between two Vector using STL in C++?
- Find and print duplicate words in std::vector<string> using STL functions
- How to erase an element from a vector using erase() and reverse_iterator?
- vector :: cbegin() and vector :: cend() in C++ STL
- vector::crend() & vector::crbegin() with example
- vector::empty() and vector::size() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- How to flatten a Vector of Vectors or 2D Vector in C++
- vector::front() and vector::back() in C++ STL
- vector::at() and vector::swap() in C++ STL
- vector::begin() and vector::end() in C++ STL
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Find three element from given three arrays such that their sum is X | Set 2
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.

