Minimum removals required to make frequency of each array element equal to its value | C # 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 ] ; Stores frequency of each element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores minimum count of removals ; Traverse the map ; Stores key value of the map ; If frequency of i is less than i ; Update cntMinRem ; If frequency of i is greater than i ; Update cntMinRem ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int min_elements ( int [ ] arr , int N ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] ++ ; } else { mp [ arr [ i ] ] = 1 ; } } int cntMinRem = 0 ; foreach ( KeyValuePair < int , int > it in mp ) { int i = it . Key ; if ( mp [ i ] < i ) { cntMinRem += mp [ i ] ; } else if ( mp [ i ] > i ) { cntMinRem += ( mp [ i ] - i ) ; } } return cntMinRem ; } static void Main ( ) { int [ ] arr = { 2 , 4 , 1 , 4 , 2 } ; int N = arr . Length ; Console . Write ( min_elements ( arr , N ) ) ; } }