Count all distinct pairs of repeating elements from the array for every array element | Function to print the required count of pairs excluding the current element ; Store the frequency ; Find all the count ; Delete the contribution of each element for equal pairs ; Print the answer ; Given array arr [ ] ; Function Call
function solve ( arr , n ) { 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 ) ; } var cnt = 0 ; mp . forEach ( ( value , key ) => { cnt += ( ( value ) * ( value - 1 ) / 2 ) ; } ) ; var ans = Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { ans [ i ] = cnt - ( mp . get ( arr [ i ] ) - 1 ) ; } for ( var i = 0 ; i < n ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } var arr = [ 1 , 1 , 2 , 1 , 2 ] ; var N = arr . length ; solve ( arr , N ) ;