Given an array which may contain duplicates, print all elements and their frequencies.
Examples:
Input : arr[] = {10, 20, 20, 10, 10, 20, 5, 20}
Output : 10 3
20 4
5 1
Input : arr[] = {10, 20, 20}
Output : 10 2
20 1
A simple solution is to run two loops. For every item count number of times it occurs. To avoid duplicate printing, keep track of processed items.
// CPP program to count frequencies of array items #include <bits/stdc++.h> using namespace std; void countFreq(int arr[], int n) { // Mark all array elements as not visited vector<bool> visited(n, false); // Traverse through array elements and // count frequencies for (int i = 0; i < n; i++) { // Skip this element if already processed if (visited[i] == true) continue; // Count frequency int count = 1; for (int j = i + 1; j < n; j++) { if (arr[i] == arr[j]) { visited[j] = true; count++; } } cout << arr[i] << " " << count << endl; } } int main() { int arr[] = { 10, 20, 20, 10, 10, 20, 5, 20 }; int n = sizeof(arr) / sizeof(arr[0]); countFreq(arr, n); return 0; } |
10 3 20 4 5 1
Time Complexity : O(n2)
Auxiliary Space : O(n)
An efficient solution is to use hashing.
// CPP program to count frequencies of array items #include <bits/stdc++.h> using namespace std; void countFreq(int arr[], int n) { unordered_map<int, int> mp; // Traverse through array elements and // count frequencies for (int i = 0; i < n; i++) mp[arr[i]]++; // Traverse through map and print frequencies for (auto x : mp) cout << x.first << " " << x.second << endl; } int main() { int arr[] = { 10, 20, 20, 10, 10, 20, 5, 20 }; int n = sizeof(arr) / sizeof(arr[0]); countFreq(arr, n); return 0; } |
5 1 10 3 20 4
Time Complexity : O(n)
Auxiliary Space : O(n)
In above efficient solution, how to print elements in same order as they appear in input?
// CPP program to count frequencies of array items #include <bits/stdc++.h> using namespace std; void countFreq(int arr[], int n) { unordered_map<int, int> mp; // Traverse through array elements and // count frequencies for (int i = 0; i < n; i++) mp[arr[i]]++; // To print elements according to first // occurrence, traverse array one more time // print frequencies of elements and mark // frequencies as -1 so that same element // is not printed multiple times. for (int i = 0; i < n; i++) { if (mp[arr[i]] != -1) { cout << arr[i] << " " << mp[arr[i]] << endl; mp[arr[i]] = -1; } } } int main() { int arr[] = { 10, 20, 20, 10, 10, 20, 5, 20 }; int n = sizeof(arr) / sizeof(arr[0]); countFreq(arr, n); return 0; } |
10 3 20 4 5 1
Time Complexity : O(n)
Auxiliary Space : O(n)
This problem can be solved in Java using Hashmap. Below is the program.
// Java prorgam to count frequencies of // integers in array using Hashmap import java.io.*; import java.util.*; class OccurenceOfNumberInArray { static void frequencyNumber(int arr[], int size) { // Creating a HashMap containing integer // as a key and occurrences as a value HashMap<Integer, Integer> freqMap = new HashMap<Integer, Integer>(); for (int i=0;i<size;i++) { if (freqMap.containsKey(arr[i])) { // If number is present in freqMap, // incrementing it's count by 1 freqMap.put(arr[i], freqMap.get(arr[i]) + 1); } else { // If integer is not present in freqMap, // putting this integer to freqMap with 1 as it's value freqMap.put(arr[i], 1); } } // Printing the freqMap for (Map.Entry entry : freqMap.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } } // Driver Code public static void main(String[] args) { int arr[] = {10, 20, 20, 10, 10, 20, 5, 20}; int size = arr.length; frequencyNumber(arr,size); } } |
Recommended Posts:
- Elements of first array that have more frequencies
- Array elements with prime frequencies
- Counting the frequencies in a list using dictionary in Python
- Find array elements with frequencies in range [l , r]
- Range Queries for Frequencies of array elements
- Sum of elements in an array with frequencies greater than or equal to that element
- Count frequencies of all elements in array in O(1) extra space and O(n) time
- Count frequencies of all elements in array in Python using collections module
- Difference between highest and least frequencies in an array
- Average of remaining elements after removing K largest and K smallest elements from array
- Find original array from encrypted array (An array of sums of other elements)
- Elements to be added so that all elements of a range are present in array
- Find elements larger than half of the elements in an array
- Find all elements in array which have at-least two greater elements
- Find elements of array using XOR of consecutive elements
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.
Improved By : Ajit kumar panigrahy



