C++ Program to Find the Frequency of Elements in an Array Last Updated : 20 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, arrays are a type of data structure that can store a fixed-size sequential collection of elements of the same type. In this article, we will learn how to find the frequency of elements in an array in C++. Example: Input: Array: {1, 2, 3, 4, 2, 1, 3, 2, 4, 5} Output: Element: 1, Frequency: 2 Element: 2, Frequency: 3 Element: 3, Frequency: 2 Element: 4, Frequency: 2 Element: 5, Frequency: 1Frequency of Elements in an Array in C++To find the frequency of all the elements in an array in C++, we can use the unordered_map with the array elements as the key and its frequency as its value. We can then traverse the array and increment the frequency of each element. ApproachCreate an unordered_map to store the frequency of each element. The keys of the map will be the elements from the array, and the corresponding values will be their frequencies.Iterate through each element of the array.For each element encountered in the array, increment its frequency count in the hash map. If the element is encountered for the first time, initialize its frequency count to 1.After traversing the entire array, iterate through the hash map. For each key-value pair in the hash map, print the element and its corresponding frequency.C++ Program to Find the Frequency of Elements in an Array C++ // C++ Program to illustrate how to find the frequency of // elements in an array #include <iostream> #include <unordered_map> using namespace std; // Driver Code int main() { // Declaring an array of integers int arr[] = { 10, 20, 20, 10, 30, 40, 10, 20 }; int n = sizeof(arr) / sizeof(arr[0]); // Declaring an unordered_map to store the frequency of // elements unordered_map<int, int> frequencyMap; // Counting the frequency of elements for (int i = 0; i < n; i++) { frequencyMap[arr[i]]++; } // Printing the frequency of elements cout << "Frequency of elements in the array is: " << endl; for (auto& i : frequencyMap) { cout << i.first << " -> " << i.second << endl; } return 0; } OutputFrequency of elements in the array is: 40 -> 1 30 -> 1 20 -> 3 10 -> 3 Time Complexity: O(N), where N is the number of array elements.Auxiliary Space: O(N) Sample Problem: Find frequencies in array Visit Course Comment More infoAdvertise with us Next Article How to Find the Frequency of an Element in a Multiset in C++? V vishaldhaygude01 Follow Improve Article Tags : C++ Programs C++ STL cpp-array cpp-unordered_map CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Find the Frequency of an Element in a Set in C++? C++ STL provides a set container that can be used to store unique elements in a sorted order. In this article, we will learn how to find the frequency of an element in a set in C++. Example: Input:set<int>s ={10,20,30,40,50}Output:Frequency of 30 is 1Finding Frequency of an Element in a Set in 2 min read How to Find Frequency of an Element in a Vector in C++? In C++, vectors are containers that store the elements in contiguous memory locations just like arrays. The frequency of a specific element means how many times that particular element occurs in a vector. In this article, we will learn how to find the frequency of a specific element in a vector in C 2 min read How to Find the Frequency of an Element in a Multiset in C++? In C++, a multiset is a container that stores elements in a sorted order and multiple elements can have the same values. In this article, we will learn how to find the frequency of a specific element in a multiset. Example: Input: myMultiset = { 5,2,8,5,8,8} Element: 8 Output: Frequency of 8 is: 3Fi 2 min read C++ Program for Range Queries for Frequencies of array elements Given an array of n non-negative integers. The task is to find frequency of a particular element in the arbitrary range of array[]. The range is given as positions (not 0 based indexes) in array. There can be multiple queries of given type. Examples:  Input : arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11}; l 4 min read How to Find Frequency of an Element in a List in C++? In C++, lists are sequence containers that allow non-contiguous memory allocation. They are implemented as doubly-linked lists. The frequency of a specific element means how many times that particular element occurs in a list. In this article, we will learn how to find the frequency of a specific el 2 min read C++ Program to Count the Dupicate Elements in an Array In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to count the duplicate elements in an array in C++. Examples: Input: myArray = {1, 2, 2, 3, 3, 3}; Output: Number of Duplicates: 3Find 3 min read Like