Replace every elements in the array by its frequency in the array
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 its frequency.
- Print the modified array.
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; } |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
Output :
1 3 2 3 3 2
Time Complexity – O(N)
Recommended Posts:
- Replace each element by the difference of the total size of the array and frequency of that element
- Sum of all odd frequency elements in an array
- Sorting Array Elements By Frequency | Set 3 (Using STL)
- Sum of elements in an array having prime frequency
- Replace every element of the array by sum of all other elements
- Replace array elements by sum of next two consecutive elements
- Array range queries for elements with frequency same as value
- Find the element having different frequency than other array elements
- Replace every element of the array by product of all other elements
- Remove elements from the array whose frequency lies in the range [l, r]
- Fill an array based on frequency where elements are in range from 0 to n-1
- Replace even nodes of a doubly linked list with the elements of array
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Find original array from encrypted array (An array of sums of other elements)
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
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.



