Number of pairs such that their HCF and LCM is equal | Function to count number of pairs such that their hcf and lcm are equal ; Store frequencies of array elements ; Count of pairs ( arr [ i ] , arr [ j ] ) where arr [ i ] = arr [ j ] ; Count of pairs ( arr [ i ] , arr [ j ] ) where arr [ i ] = arr [ j ] , ; Driver function
function countPairs ( arr , n ) { let frequency = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( frequency . has ( arr [ i ] ) ) { frequency . set ( arr [ i ] , frequency . get ( arr [ i ] ) + 1 ) ; } else { frequency . set ( arr [ i ] , 1 ) ; } } let count = 0 ; for ( let [ key , value ] of frequency . entries ( ) ) { let f = value ; count += f * ( f - 1 ) / 2 ; } return count ; } let arr = [ 1 , 1 , 1 ] ; let n = arr . length ; document . write ( countPairs ( arr , n ) ) ;