Remove an occurrence of most frequent array element exactly K times | Function to print the most frequent array element exactly K times ; Stores frequency array element ; Count frequency of array element ; Maximum array element ; Traverse the Map ; Find the element with maximum frequency ; If the frequency is maximum , store that number in element ; Print element as it contains the element having highest frequency ; Decrease the frequency of the maximum array element ; Reduce the number of operations ; Given array ; Size of the array ; Given K
def maxFreqElements ( arr , N , K ) : 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 while ( K > 0 ) : NEW_LINE INDENT Max = 0 NEW_LINE for i in mp : NEW_LINE INDENT if ( mp [ i ] > Max ) : NEW_LINE INDENT Max = mp [ i ] NEW_LINE element = i NEW_LINE DEDENT DEDENT print ( element , end = " ▁ " ) NEW_LINE if element in mp : NEW_LINE INDENT mp [ element ] -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ element ] = - 1 NEW_LINE DEDENT K -= 1 NEW_LINE DEDENT DEDENT arr = [ 1 , 3 , 2 , 1 , 4 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE K = 2 NEW_LINE maxFreqElements ( arr , N , K ) NEW_LINE