Minimum number of distinct elements after removing M items | Set 2 | C # program to implement the above approach ; Function to return minimum distinct character after M removals ; Count the occurences of number and store in count ; Count the occurences of the frequencies ; Take answer as total unique numbers and remove the frequency and subtract the answer ; Remove the minimum number of times ; Return the answer ; Driver code ; Initialize array ; Size of array ; Function call
using System ; using System . Collections . Generic ; class GFG { static int distinctNumbers ( int [ ] arr , int m , int n ) { Dictionary < int , int > count = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( count . ContainsKey ( arr [ i ] ) ) { count [ arr [ i ] ] = count [ arr [ i ] ] + 1 ; } else { count . Add ( arr [ i ] , 1 ) ; } int [ ] fre_arr = new int [ n + 1 ] ; foreach ( int it in count . Values ) { fre_arr [ it ] ++ ; } int ans = count . Count ; for ( int i = 1 ; i <= n ; i ++ ) { int temp = fre_arr [ i ] ; if ( temp == 0 ) continue ; int t = Math . Min ( temp , m / i ) ; ans -= t ; m -= i * t ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 1 , 5 , 3 , 5 , 1 , 3 } ; int n = arr . Length ; int m = 2 ; Console . WriteLine ( distinctNumbers ( arr , m , n ) ) ; } }