Related Articles

# Minimum removals required to make frequency of each array element equal to its value

• Last Updated : 18 May, 2021

Given an array arr[] of size N, the task is to find the minimum count of array elements required to be removed such that frequency of each array element is equal to its value

Examples:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr[] = { 2, 4, 1, 4, 2 }
Output:
Explanation:
Removing arr from the array modifies arr[] to { 2, 1, 4, 2 }
Removing arr from the array modifies arr[] to { 2, 1, 2 }
Distinct elements in the array are: { 1, 2 } with frequencies 1 and 2 respectively.
Therefore, the required output is 2.

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

Approach: The problem can be solved using Greedy technique. Follow the steps below to solve the problem:

• Initialize a map say, mp to store the frequency of each distinct element of the array.
• Traverse the array and store the frequency of each distinct element of the array.
• Initialize a variable say, cntMinRem to store the minimum count of array elements required to be removed such that the frequency of arr[i] is equal to arr[i].
• Traverse the map using key value of the map as i and check the following conditions:
• If mp[i] < i, then update the value of cntMinRem += mp[i].
• If mp[i] > i, then update the value of cntMinRem += (mp[i] – i)

• Finally, print the value of cntMinRem.

Below is the implementation of the above approach:

## C++14

 `// C++ program to implement``// the above approach` `#include ``using` `namespace` `std;` `// Function to find the minimum count of``// elements required to be removed such``// that frequency of arr[i] equal to arr[i]``int` `min_elements(``int` `arr[], ``int` `N)``{``    ``// Stores frequency of each``    ``// element of the array``    ``unordered_map<``int``, ``int``> mp;` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// Update frequency``        ``// of arr[i]``        ``mp[arr[i]]++;``    ``}` `    ``// Stores minimum count of removals``    ``int` `cntMinRem = 0;` `    ``// Traverse the map``    ``for` `(``auto` `it : mp) {` `        ``// Stores key value``        ``// of the map``        ``int` `i = it.first;` `        ``// If frequency of i is``        ``// less than i``        ``if` `(mp[i] < i) {` `            ``// Update cntMinRem``            ``cntMinRem += mp[i];``        ``}` `        ``// If frequency of i is``        ``// greater than i``        ``else` `if` `(mp[i] > i) {` `            ``// Update cntMinRem``            ``cntMinRem += (mp[i] - i);``        ``}``    ``}` `    ``return` `cntMinRem;``}` `// Driver Code``int` `main()``{` `    ``int` `arr[] = { 2, 4, 1, 4, 2 };` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);` `    ``cout << min_elements(arr, N);``    ``return` `0;``}`

## Java

 `// Java program to implement``// the above approach``import` `java.util.*;` `class` `GFG{``    ` `// Function to find the minimum count of``// elements required to be removed such``// that frequency of arr[i] equal to arr[i]``public` `static` `int` `min_elements(``int` `arr[], ``int` `N)``{``    ` `    ``// Stores frequency of each``    ``// element of the array``    ``Map mp = ``new` `HashMap();` `    ``// Traverse the array``    ``for``(``int` `i = ``0``; i < N; i++)``    ``{``        ` `        ``// Update frequency``        ``// of arr[i]``        ``mp.put(arr[i],``               ``mp.getOrDefault(arr[i], ``0``) + ``1``);``    ``}` `    ``// Stores minimum count of removals``    ``int` `cntMinRem = ``0``;` `    ``// Traverse the map``    ``for``(``int` `key : mp.keySet())``    ``{``        ` `        ``// Stores key value``        ``// of the map``        ``int` `i = key;``        ``int` `val = mp.get(i);` `        ``// If frequency of i is``        ``// less than i``        ``if` `(val < i)``        ``{``            ` `            ``// Update cntMinRem``            ``cntMinRem += val;``        ``}` `        ``// If frequency of i is``        ``// greater than i``        ``else` `if` `(val > i)``        ``{``            ` `            ``// Update cntMinRem``            ``cntMinRem += (val - i);``        ``}``    ``}``    ``return` `cntMinRem;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `arr[] = { ``2``, ``4``, ``1``, ``4``, ``2` `};` `    ``System.out.println(min_elements(``        ``arr, arr.length));``}``}` `// This code is contributed by grand_master`

## Python3

 `# Python3 program to implement``# the above approach` `# Function to find the minimum count of``# elements required to be removed such``# that frequency of arr[i] equal to arr[i]``def` `min_elements(arr, N) :``    ` `    ``# Stores frequency of each``    ``# element of the array``    ``mp ``=` `{};` `    ``# Traverse the array``    ``for` `i ``in` `range``(N) :` `        ``# Update frequency``        ``# of arr[i]``        ``if` `arr[i] ``in` `mp :``            ``mp[arr[i]] ``+``=` `1``;``        ``else` `:``            ``mp[arr[i]] ``=` `1``;` `    ``# Stores minimum count of removals``    ``cntMinRem ``=` `0``;` `    ``# Traverse the map``    ``for` `it ``in` `mp :` `        ``# Stores key value``        ``# of the map``        ``i ``=` `it;` `        ``# If frequency of i is``        ``# less than i``        ``if` `(mp[i] < i) :` `            ``# Update cntMinRem``            ``cntMinRem ``+``=` `mp[i];` `        ``# If frequency of i is``        ``# greater than i``        ``elif` `(mp[i] > i) :` `            ``# Update cntMinRem``            ``cntMinRem ``+``=` `(mp[i] ``-` `i);``            ` `    ``return` `cntMinRem;` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:` `    ``arr ``=` `[ ``2``, ``4``, ``1``, ``4``, ``2` `];``    ``N ``=` `len``(arr);``    ``print``(min_elements(arr, N));``    ` `    ``# This code is contributed by AnkThon`

## C#

 `// C# program to implement``// the above approach``using` `System;``using` `System.Collections.Generic;``class` `GFG``{``    ` `    ``// Function to find the minimum count of``    ``// elements required to be removed such``    ``// that frequency of arr[i] equal to arr[i]``    ``static` `int` `min_elements(``int``[] arr, ``int` `N)``    ``{``      ` `        ``// Stores frequency of each``        ``// element of the array``        ``Dictionary<``int``, ``int``> mp = ``new` `Dictionary<``int``, ``int``>(); ``     ` `        ``// Traverse the array``        ``for` `(``int` `i = 0; i < N; i++)``        ``{``     ` `            ``// Update frequency``            ``// of arr[i]``            ``if``(mp.ContainsKey(arr[i]))``            ``{``                ``mp[arr[i]]++;``            ``}``            ``else``            ``{``                ``mp[arr[i]] = 1;``            ``}``        ``}``     ` `        ``// Stores minimum count of removals``        ``int` `cntMinRem = 0;``     ` `        ``// Traverse the map``        ``foreach` `(KeyValuePair<``int``, ``int``> it ``in` `mp)``        ``{``     ` `            ``// Stores key value``            ``// of the map``            ``int` `i = it.Key;``     ` `            ``// If frequency of i is``            ``// less than i``            ``if` `(mp[i] < i)``            ``{``     ` `                ``// Update cntMinRem``                ``cntMinRem += mp[i];``            ``}``     ` `            ``// If frequency of i is``            ``// greater than i``            ``else` `if` `(mp[i] > i)``            ``{``     ` `                ``// Update cntMinRem``                ``cntMinRem += (mp[i] - i);``            ``}``        ``}    ``        ``return` `cntMinRem;``    ``}` `  ``// Driver code``  ``static` `void` `Main()``  ``{``    ``int``[] arr = { 2, 4, 1, 4, 2 };``    ``int` `N = arr.Length;``    ``Console.Write(min_elements(arr, N));``  ``}``}` `// This code is contributed by divyesh072019`

## Javascript

 ``
Output:
`2`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up