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 ] ; Given Array
function pairs_after_removing ( arr , N ) { var cntPairs = 0 ; var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } mp . forEach ( ( value , key ) => { var i = key ; cntPairs += mp . get ( i ) * ( mp . get ( i ) - 1 ) / 2 ; } ) ; for ( var i = 0 ; i < N ; i ++ ) { var pairs_after_arr_i_removed = cntPairs + 1 - mp . get ( arr [ i ] ) ; document . write ( pairs_after_arr_i_removed + ' ' ) ; } return ; } var arr = [ 2 , 3 , 4 , 3 , 2 ] ; var N = arr . length ; pairs_after_removing ( arr , N ) ;