Replace every elements in the array by its frequency in the array Last Updated : 17 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Given an array of integers, replace every element by its frequency in the array. Examples: Input : arr[] = { 1, 2, 5, 2, 2, 5 } Output : 1 3 2 3 3 2 Input : arr[] = { 4 5 4 5 6 6 6 } Output : 2 2 2 2 3 3 3 Approach: Take a hash map, which will store the frequency of all the elements in the array.Now, traverse once again.Now, replace all the elements by their frequency.Print the modified array. Implementation: C++ // C++ program to replace the elements // by their frequency in the array. #include "iostream" #include "unordered_map" using namespace std; void ReplaceElementsByFrequency(int arr[], int n) { // Hash map which will store the // frequency of the elements of the array. unordered_map<int, int> mp; for (int i = 0; i < n; ++i) { // Increment the frequency // of the element by 1. mp[arr[i]]++; } // Replace every element by its frequency for (int i = 0; i < n; ++i) { arr[i] = mp[arr[i]]; } } int main() { int arr[] = { 1, 2, 5, 2, 2, 5 }; int n = sizeof(arr) / sizeof(arr[0]); ReplaceElementsByFrequency(arr, n); // Print the modified array. for (int i = 0; i < n; ++i) { cout << arr[i] << " "; } return 0; } Java import java.util.HashMap; // Java program to replace the elements // by their frequency in the array. class GFG { static void ReplaceElementsByFrequency(int arr[], int n) { // Hash map which will store the // frequency of the elements of the array. HashMap<Integer, Integer> mp = new HashMap<Integer, Integer>(); for (int i = 0; i < n; ++i) { // Increment the frequency // of the element by 1. if (mp.get(arr[i]) == null) { mp.put(arr[i], 1); } else { mp.put(arr[i], (mp.get(arr[i]) + 1)); } //mp[arr[i]]++; } // Replace every element by its frequency for (int i = 0; i < n; ++i) { if (mp.get(arr[i]) != null) { arr[i] = mp.get(arr[i]); } //arr[i] = mp[arr[i]]; } } public static void main(String[] args) { int arr[] = {1, 2, 5, 2, 2, 5}; int n = arr.length; ReplaceElementsByFrequency(arr, n); // Print the modified array. for (int i = 0; i < n; ++i) { System.out.print(arr[i] + " "); } } } // This code contributed by 29AJayKumar Python3 # Python 3 program to replace the elements # by their frequency in the array. def ReplaceElementsByFrequency(arr, n): # Hash map which will store the # frequency of the elements of the array. mp = {i:0 for i in range(len(arr))} for i in range(n): # Increment the frequency of the # element by 1. mp[arr[i]] += 1 # Replace every element by its frequency for i in range(n): arr[i] = mp[arr[i]] # Driver Code if __name__ == '__main__': arr = [1, 2, 5, 2, 2, 5] n = len(arr) ReplaceElementsByFrequency(arr, n); # Print the modified array. for i in range(n): print(arr[i], end = " ") # This code is contributed by # Sahil_shelangia C# // C# program to replace the elements // by their frequency in the array. using System; using System.Collections.Generic; class GFG { static void ReplaceElementsByFrequency(int []arr, int n) { // Hash map which will store the // frequency of the elements of the array. Dictionary<int,int> mp = new Dictionary<int,int>(); for (int i = 0; i < n; ++i) { // Increment the frequency // of the element by 1. if (!mp.ContainsKey(arr[i])) { mp.Add(arr[i], 1); } else { var a = mp[arr[i]] + 1; mp.Remove(arr[i]); mp.Add(arr[i], a); } } // Replace every element by its frequency for (int i = 0; i < n; ++i) { if (mp[arr[i]] != 0) { arr[i] = mp[arr[i]]; } //arr[i] = mp[arr[i]]; } } // Driver code public static void Main(String[] args) { int []arr = {1, 2, 5, 2, 2, 5}; int n = arr.Length; ReplaceElementsByFrequency(arr, n); // Print the modified array. for (int i = 0; i < n; ++i) { Console.Write(arr[i] + " "); } } } // This code contributed by Rajput-Ji JavaScript <script> // JavaScript program to replace the elements // by their frequency in the array. function ReplaceElementsByFrequency(arr, n) { // Hash map which will store the // frequency of the elements of the array. let mp = new Map(); for(let i = 0; i < n; ++i) { // Increment the frequency // of the element by 1. if (mp.get(arr[i]) == null) { mp.set(arr[i], 1); } else { mp.set(arr[i], (mp.get(arr[i]) + 1)); } //mp[arr[i]]++; } // Replace every element by its frequency for(let i = 0; i < n; ++i) { if (mp.get(arr[i]) != null) { arr[i] = mp.get(arr[i]); } //arr[i] = mp[arr[i]]; } } // Driver Code let arr = [ 1, 2, 5, 2, 2, 5 ]; let n = arr.length; ReplaceElementsByFrequency(arr, n); // Print the modified array. for(let i = 0; i < n; ++i) { document.write(arr[i] + " "); } // This code is contributed by code_hunt </script> Output1 3 2 3 3 2 Time Complexity: O(N) Auxiliary Space: O(N) because extra space is being used for unordered_map mp Comment More infoAdvertise with us Next Article Replace every elements in the array by its frequency in the array I imdhruvgupta Follow Improve Article Tags : Hash Technical Scripter DSA Arrays Technical Scripter 2018 cpp-unordered_map frequency-counting +3 More Practice Tags : ArraysHash Similar Reads Find the frequency of each element in a sorted array Given a sorted array, arr[] consisting of N integers, the task is to find the frequencies of each array element. Examples: Input: arr[] = {1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10} Output: Frequency of 1 is: 3 Frequency of 2 is: 1 Frequency of 3 is: 2 Frequency of 5 is: 2 Frequency of 8 is: 3 Frequ 10 min read Minimize the maximum frequency of Array elements by replacing them only once Given an array A[] of length N, the task is to minimize the maximum frequency of any array element by performing the following operation only once: Choose any random set (say S) of indices i (0 ? i ? N-1) of the array such that every element of S is the same and Replace every element of that index w 7 min read Fill an array based on frequency where elements are in range from 0 to n-1 Given an array of positive integers with duplicate allowed. The array contains elements from range 0 to n-1. The task is to fill the array such that arr[i] contains the frequency of i. Examples : Input : arr[] = {1, 4, 3, 4, 1, 1, 4, 4, 4, 7} Output : arr[] = {0, 3, 0, 1, 5, 0, 0, 1, 0, 0} Here 0 ap 5 min read Count frequencies of all elements in array in O(1) extra space and O(n) time Given an unsorted array of n integers that can contain integers from 1 to n. Some elements can be repeated multiple times and some other elements can be absent from the array. Count the frequency of all elements that are present and print the missing elements. Examples: Input: arr[] = {2, 3, 3, 2, 5 15+ min read Find frequency of each element in a limited range array in less than O(n) time Given a sorted array arr[] of positive integers, the task is to find the frequency for each element in the array. Assume all elements in the array are less than some constant MNote: Do this without traversing the complete array. i.e. expected time complexity is less than O(n)Examples: Input: arr[] = 10 min read Like