text: Minimum deletions required such that any number X will occur exactly X times | C # Implementation of above approach ; Function to return the minimum deletions required ; To store the frequency of the array elements ; Store frequency of each element ; To store the minimum deletions required ; Value ; It 's frequency ; If number less than or equal to it 's frequency ; Delete extra occurrences ; Delete every occurrence of x ; Driver code
code: using System ; using System . Collections . Generic ; class GFG { static int MinDeletion ( int [ ] a , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( a [ i ] ) ) { var val = mp [ a [ i ] ] ; mp . Remove ( a [ i ] ) ; mp . Add ( a [ i ] , val + 1 ) ; } else { mp . Add ( a [ i ] , 1 ) ; } } int ans = 0 ; foreach ( KeyValuePair < int , int > i in mp ) { int x = i . Key ; int frequency = i . Value ; if ( x <= frequency ) { ans += ( frequency - x ) ; } else ans += frequency ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 3 , 2 , 3 , 4 , 4 , 4 , 4 , 5 } ; int n = a . Length ; Console . WriteLine ( MinDeletion ( a , n ) ) ; } }