text: Count pairs of equal elements possible by excluding each array element once | package whatever do not write package name here ; 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 ; Driver code ; Given array ; Size of the array
code: import java . io . * ; import java . util . Map ; import java . util . HashMap ; class GFG { public static void countEqualElementPairs ( int arr [ ] , int N ) { HashMap < Integer , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Integer k = map . get ( arr [ i ] ) ; map . put ( arr [ i ] , ( k == null ) ? 1 : k + 1 ) ; } int total = 0 ; for ( Map . Entry < Integer , Integer > e : map . entrySet ( ) ) { total += ( e . getValue ( ) * ( e . getValue ( ) - 1 ) ) / 2 ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( total - ( map . get ( arr [ i ] ) - 1 ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 1 , 2 } ; int N = 5 ; countEqualElementPairs ( arr , N ) ; } }