Minimum removals required to make frequency of each array element equal to its value | 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
def min_elements ( arr , N ) : NEW_LINE INDENT mp = { } ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT if arr [ i ] in mp : NEW_LINE INDENT mp [ arr [ i ] ] += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] ] = 1 ; NEW_LINE DEDENT DEDENT cntMinRem = 0 ; NEW_LINE for it in mp : NEW_LINE INDENT i = it ; NEW_LINE if ( mp [ i ] < i ) : NEW_LINE INDENT cntMinRem += mp [ i ] ; NEW_LINE DEDENT elif ( mp [ i ] > i ) : NEW_LINE INDENT cntMinRem += ( mp [ i ] - i ) ; NEW_LINE DEDENT DEDENT return cntMinRem ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 1 , 4 , 2 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( min_elements ( arr , N ) ) ; NEW_LINE DEDENT