Count pairs of equal elements possible by excluding each array element once | C # program for the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static void countEqualElementPairs ( int [ ] arr , int N ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! map . ContainsKey ( arr [ i ] ) ) map [ arr [ i ] ] = 1 ; else map [ arr [ i ] ] ++ ; } int total = 0 ; foreach ( KeyValuePair < int , int > e in map ) { total += ( e . Value * ( e . Value - 1 ) ) / 2 ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( total - ( map [ arr [ i ] ] - 1 ) + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 1 , 2 } ; int N = 5 ; countEqualElementPairs ( arr , N ) ; } }