Given an array **arr[]** of size **N**, the task is to find the minimum number of array elements required to be removed such that the frequency of the remaining array elements become equal.

**Examples :**

Input:arr[] = {2, 4, 3, 2, 5, 3}Output:2Explanation:Following two possibilities exists:

1) Either remove an occurrence of 2 and 3. The array arr[] modifies to {2, 4, 3, 5}. Therefore, frequency of all the elements become equal.

2) Or, remove an occurrence of 4 and 5. The array arr[] modifies to {2, 3, 2, 3}. Therefore, frequency of all the elements become equal.

Input:arr[] = {1, 1, 2, 1}Output:1

**Approach: **Follow the steps below to solve the problem:

- Initialize an ordered map, say
**freq,**to store the frequency of all array elements. - Traverse the array
**arr[]**and store the respective frequencies of array elements. - Initialize a vector, say
**v,**to store all the frequencies stored in the map. - Sort the vector
**v.** - Initialize a variable, say
**ans,**to store the final count. - Traverse the vector
**v**and for each frequency, count the number of elements required to be removed to make the frequency of the remaining elements equal. - Print the minimum of all the count of removals obtained.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count the minimum` `// removals required to make frequency` `// of all array elements equal` `int` `minDeletions(` `int` `arr[], ` `int` `N)` `{` ` ` `// Stores frequency of` ` ` `// all array elements` ` ` `map<` `int` `, ` `int` `> freq;` ` ` `// Traverse the array` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `freq[arr[i]]++;` ` ` `}` ` ` `// Stores all the frequencies` ` ` `vector<` `int` `> v;` ` ` `// Traverse the map` ` ` `for` `(` `auto` `z : freq) {` ` ` `v.push_back(z.second);` ` ` `}` ` ` `// Sort the frequencies` ` ` `sort(v.begin(), v.end());` ` ` `// Count of frequencies` ` ` `int` `size = v.size();` ` ` `// Stores the final count` ` ` `int` `ans = N - (v[0] * size);` ` ` `// Traverse the vector` ` ` `for` `(` `int` `i = 1; i < v.size(); i++) {` ` ` `// Count the number of removals` ` ` `// for each frequency and update` ` ` `// the minimum removals required` ` ` `if` `(v[i] != v[i - 1]) {` ` ` `int` `safe = v[i] * (size - i);` ` ` `ans = min(ans, N - safe);` ` ` `}` ` ` `}` ` ` `// Print the final count` ` ` `cout << ans;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given array` ` ` `int` `arr[] = { 2, 4, 3, 2, 5, 3 };` ` ` `// Size of the array` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `// Function call to print the minimum` ` ` `// number of removals required` ` ` `minDeletions(arr, N);` `}` |

## Python3

`# Python 3 program for the above approach` `from` `collections ` `import` `defaultdict` `# Function to count the minimum` `# removals required to make frequency` `# of all array elements equal` `def` `minDeletions(arr, N):` ` ` ` ` `# Stores frequency of` ` ` `# all array elements` ` ` `freq ` `=` `defaultdict(` `int` `)` ` ` `# Traverse the array` ` ` `for` `i ` `in` `range` `(N):` ` ` `freq[arr[i]] ` `+` `=` `1` ` ` `# Stores all the frequencies` ` ` `v ` `=` `[]` ` ` `# Traverse the map` ` ` `for` `z ` `in` `freq.keys():` ` ` `v.append(freq[z])` ` ` `# Sort the frequencies` ` ` `v.sort()` ` ` `# Count of frequencies` ` ` `size ` `=` `len` `(v)` ` ` `# Stores the final count` ` ` `ans ` `=` `N ` `-` `(v[` `0` `] ` `*` `size)` ` ` `# Traverse the vector` ` ` `for` `i ` `in` `range` `(` `1` `, ` `len` `(v)):` ` ` `# Count the number of removals` ` ` `# for each frequency and update` ` ` `# the minimum removals required` ` ` `if` `(v[i] !` `=` `v[i ` `-` `1` `]):` ` ` `safe ` `=` `v[i] ` `*` `(size ` `-` `i)` ` ` `ans ` `=` `min` `(ans, N ` `-` `safe)` ` ` `# Print the final count` ` ` `print` `(ans)` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `# Given array` ` ` `arr ` `=` `[` `2` `, ` `4` `, ` `3` `, ` `2` `, ` `5` `, ` `3` `]` ` ` `# Size of the array` ` ` `N ` `=` `len` `(arr)` ` ` `# Function call to print the minimum` ` ` `# number of removals required` ` ` `minDeletions(arr, N)` ` ` `# This code is contributed by chitranayal.` |

**Output:**

2

**Time Complexity: **O(N) **Auxiliary Space:** 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.