Count of index pairs with equal elements in an array | C # program to count of index pairs with equal elements in an array . ; Return the number of pairs with equal values . ; Finding frequency of each number . ; Calculating count of pairs with equal values ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static int countPairs ( int [ ] arr , int n ) { Dictionary < int , int > hm = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hm . ContainsKey ( arr [ i ] ) ) { int a = hm [ arr [ i ] ] ; hm . Remove ( arr [ i ] ) ; hm . Add ( arr [ i ] , a + 1 ) ; } else hm . Add ( arr [ i ] , 1 ) ; } int ans = 0 ; foreach ( var it in hm ) { int count = it . Value ; ans += ( count * ( count - 1 ) ) / 2 ; } return ans ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 1 } ; Console . WriteLine ( countPairs ( arr , arr . Length ) ) ; } }