Count pairs of equal elements possible by excluding each array element once | Function to count the number of required pairs for every array element ; Initialize a map ; Update the frequency of every element ; Stores the count of pairs ; Traverse the map ; Count the number of ways to select pairs consisting of equal elements only ; Traverse the array ; Print the count for every array element ; Given array ; Size of the array
function countEqualElementPairs ( 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 total = 0 ; mp . forEach ( ( value , key ) => { total += ( value * ( value - 1 ) ) / 2 ; } ) ; for ( var i = 0 ; i < N ; i ++ ) { document . write ( total - ( mp . get ( arr [ i ] ) - 1 ) + " " ) ; } } var arr = [ 1 , 1 , 2 , 1 , 2 ] ; var N = arr . length ; countEqualElementPairs ( arr , N ) ;