Given an array of integers, the task is to replace every element by the difference of the total size of the array and its frequency.

**Examples:**

Input:arr[] = { 1, 2, 5, 2, 2, 5, 4 }Output:6 4 5 4 4 5 6 Size of the array is 7. The frequency of 1 is 1. So replace it by 7-1 = 6 The frequency of 2 is 3. So replace it by 7-3 = 4Input:arr[] = { 4, 5, 4, 5, 6, 6, 6 }Output:5 5 5 5 4 4 4

**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 the difference of the total size of the array and its frequency.
- Print the modified array.

Below si the implementation of the above approach:

## C++

`// C++ program to Replace each element ` `// by the difference of the total size ` `// of the array and its frequency ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to replace the elements ` `void` `ReplaceElements(` `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] = n - mp[arr[i]]; ` ` ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 5, 2, 2, 5, 4 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `ReplaceElements(arr, n); ` ` ` ` ` `// Print the modified array. ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `cout << arr[i] << ` `" "` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to Replace each element ` `// by the difference of the total size ` `// of the array and its frequency ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to replace the elements ` ` ` `static` `void` `ReplaceElements(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` `// Hash map which will store the ` ` ` `// frequency of the elements of the array. ` ` ` `HashMap<Integer, Integer> mp = ` `new` `HashMap<>(); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// Increment the frequency ` ` ` `// of the element by 1. ` ` ` `if` `(!mp.containsKey(arr[i])) ` ` ` `{ ` ` ` `mp.put(arr[i], ` `1` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `mp.put(arr[i], mp.get(arr[i]) + ` `1` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Replace every element by its frequency ` ` ` `for` `(` `int` `i = ` `0` `; i < n; ++i) ` ` ` `{ ` ` ` `arr[i] = n - mp.get(arr[i]); ` ` ` `} ` ` ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `arr[] = {` `1` `, ` `2` `, ` `5` `, ` `2` `, ` `2` `, ` `5` `, ` `4` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `ReplaceElements(arr, n); ` ` ` ` ` `// Print the modified array. ` ` ` `for` `(` `int` `i = ` `0` `; i < n; ++i) ` ` ` `{ ` ` ` `System.out.print(arr[i] + ` `" "` `); ` ` ` `} ` ` ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to Replace each element ` `# by the difference of the total size ` `# of the array and its frequency ` ` ` `# Function to replace the elements ` `def` `ReplaceElements(arr, n): ` ` ` ` ` `# Hash map which will store the ` ` ` `# frequency of the elements of the array. ` ` ` `mp ` `=` `dict` `() ` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# Increment the frequency ` ` ` `# of the element by 1. ` ` ` `mp[arr[i]] ` `=` `mp.get(arr[i], ` `0` `) ` `+` `1` ` ` ` ` `# Replace every element by its frequency ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `arr[i] ` `=` `n ` `-` `mp[arr[i]] ` ` ` `# Driver code ` `arr ` `=` `[` `1` `, ` `2` `, ` `5` `, ` `2` `, ` `2` `, ` `5` `, ` `4` `] ` `n ` `=` `len` `(arr) ` ` ` `ReplaceElements(arr, n) ` ` ` `# Print the modified array. ` `for` `i ` `in` `range` `(n): ` ` ` `print` `(arr[i], end ` `=` `" "` `) ` ` ` `# This code is contributed by mohit kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to Replace each element ` `// by the difference of the total size ` `// of the array and its frequency ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to replace the elements ` ` ` `static` `void` `ReplaceElements(` `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) ` ` ` `{ ` ` ` `arr[i] = n - mp[arr[i]]; ` ` ` `} ` ` ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]arr = {1, 2, 5, 2, 2, 5, 4}; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `ReplaceElements(arr, n); ` ` ` ` ` `// Print the modified array. ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `{ ` ` ` `Console.Write(arr[i] + ` `" "` `); ` ` ` `} ` ` ` `} ` `} ` ` ` `/* This code contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

**Output:**

6 4 5 4 4 5 6

**Time Complexity –** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Form an array of distinct elements with each element as sum of an element from each array
- Replace every elements in the array by its frequency in the array
- Replace each element of Array with it's corresponding rank
- Find frequency of each element in a limited range array in less than O(n) time
- Cumulative frequency of count of each element in an unsorted array
- Frequency of each element of an array of small ranged values
- Check if frequency of each element in given array is unique or not
- Array formed from difference of each element from the largest element in the given array
- Replace elements with absolute difference of smallest element on left and largest element on right
- Sum of product of each element with each element after it
- Find Array formed by adding each element of given array with largest element in new array to its left
- Replace every array element by Bitwise Xor of previous and next element
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Replace each node in binary tree with the sum of its inorder predecessor and successor
- Minimum element of each row and each column in a matrix
- Replace every element of the array by its previous element

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.