Find the Frequency of Each Element in a Vector in C++ Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The frequency of an element is number of times it occurred in the vector. In this article, we will learn different methods to find the frequency of each element in a vector in C++.The simplest method to find the frequency of each element in a vector is by iterating the vector and storing the count of each element in an unordered_map. Let’s take a look at an example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {'a', 'b', 'd', 'c', 'a', 'b', 'b'}; unordered_map<char, int> um; // Calculate frequency of each element of vector for (auto i : v) um[i]++; for (auto i : um) cout << i.first << ": " << i.second << endl; return 0; } Outputc: 1 d: 1 b: 3 a: 2 Note: If we want to print the elements in sorted order then we have to use map container, but the average time complexity of map insertion is O(log n) whereas O(1) for unordered_map.There are also some other methods in C++ to find the frequency of each element in vector. Some of them are as follows:Using ArrayIf the vector elements are of integer type, the frequency of each element of a vector can also be counted in an array. The index can be considered to be the vector's element and the value at this index can be used to store the frequency. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 3, 2, 1, 5, 1, 3}; // Find maximum element int m = *max_element(v.begin(), v.end()); // Initialize array for counting frequency int f[m + 1] = {0}; // Count frequency of every element for (auto i : v) f[i]++; for (int i = 0; i <= m; i++) { // Check if element exist in original vector // or not if (f[i] != 0) cout << i << ": " << f[i] << endl; } return 0; } Output1: 3 2: 1 3: 2 5: 1 The disadvantage of this elements is that if the number of elements is much less than the largest element in the vector, it wastes a lot of memory.Note: This Method will not work for negative elements in vector, gives error as the index of vector can't be negative.Using sort()To count the frequency of each element, first sort the vector using sort() function to group identical elements together. Then iterate through the sorted vector and count consecutive occurrences of each element. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 3, 2, 1, 5, 1, 3}; sort(v.begin(), v.end()); int i = 0; // Counting the frequency of every elements while (i < v.size()) { int c = 1, e = v[i]; i++; while (i < v.size()) { if (v[i] == v[i - 1]) c++; else break; i++; } cout << e << ": " << c << endl; } return 0; } Output1: 3 2: 1 3: 2 5: 1 Comment More infoAdvertise with us Next Article Count elements in a vector that match a target value or condition S ShivaTeja2 Follow Improve Article Tags : C++ cpp-map Practice Tags : CPP Similar Reads Find frequency of each element in given Matrix Given a matrix arr[] of size N*N containing English alphabets characters, the task is to find the frequency of all the matrix elements. Note: Print them in the decreasing order of the frequency. Examples: Input: N = 2, arr[] = {{'a', 'b'}, {'a', 'c'}}Output: a : 2, b : 1, c : 1Explanation: The frequ 6 min read Count the elements having frequency equals to its value | Set 2 Given an array of integers arr[] of size N, the task is to count all the elements of the array which have a frequency equals to its value.Examples: Input: arr[] = {3, 2, 2, 3, 4, 3} Output: 2 Explanation : Frequency of element 2 is 2 Frequency of element 3 is 3 Frequency of element 4 is 1 2 and 3 ar 5 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 Count occurrences of the average of array elements with a given number Given an array of N integers and an integer x . For every integer of the array a[i], the task is to calculate the count of numbers in the array with value equals to the average of element a[i] and x. That is, the number of occurrences of the (average of element a[i] and x) in the array. Examples: In 7 min read Count elements in a vector that match a target value or condition To determine number of integers in a vector that matches a particular value. We use count in C++ STL CPP // CPP program to count vector elements that // match given target value. #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { vector< 1 min read How to Erase Duplicates and Sort a Vector in C++? In this article, we will learn how to remove duplicates and sort a vector in C++.The simplest method to remove the duplicates and sort the vector is by using sort() and unique() functions. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { vector<i 3 min read Like