Minimum number of distinct elements after removing M items | Set 2 | 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
def distinctNumbers ( arr , m , n ) : NEW_LINE INDENT count = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT count [ arr [ i ] ] = count . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT fre_arr = [ 0 ] * ( n + 1 ) NEW_LINE for it in count : NEW_LINE INDENT fre_arr [ count [ it ] ] += 1 NEW_LINE DEDENT ans = len ( count ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT temp = fre_arr [ i ] NEW_LINE if ( temp == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT t = min ( temp , m // i ) NEW_LINE ans -= t NEW_LINE m -= i * t NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 1 , 5 , 3 , 5 , 1 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE m = 2 NEW_LINE print ( distinctNumbers ( arr , m , n ) ) NEW_LINE DEDENT