Count pairs of equal array elements remaining after every removal | Function to count pairs of equal elements by removing arr [ i ] from the array ; Stores total count of pairs of equal elements ; Store frequency of each distinct array element ; Traverse the array ; Update frequency of arr [ i ] ; Traverse the map ; Stores key of an element ; Traverse the array ; Stores count of pairs of equal element by removing arr [ i ] ; Driver Code ; Given Array
def pairs_after_removing ( arr , N ) : NEW_LINE INDENT cntPairs = 0 NEW_LINE mp = { } NEW_LINE for i in arr : NEW_LINE INDENT mp [ i ] = mp . get ( i , 0 ) + 1 NEW_LINE DEDENT for element in mp : NEW_LINE INDENT i = element NEW_LINE cntPairs += mp [ i ] * ( mp [ i ] - 1 ) // 2 NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT pairs_after_arr_i_removed = cntPairs + 1 - mp [ arr [ i ] ] NEW_LINE print ( pairs_after_arr_i_removed , end = ' ▁ ' ) NEW_LINE DEDENT return NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 3 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE pairs_after_removing ( arr , N ) NEW_LINE DEDENT