Count pairs of equal array elements remaining after every removal | C # program to implement the above approach ; Function to count pairs of equal elements by removing arr [ i ] from the array ; Stores total count of pairs of equal elements ; Store frequency of each distinct array element ; Traverse the array ; Update frequency of arr [ i ] ; Traverse the map ; Stores key of an element ; Traverse the array ; Stores count of pairs of equal element by removing arr [ i ] ; Driver code ; Given Array
using System ; using System . Collections . Generic ; class GFG { static void pairs_after_removing ( int [ ] arr , int N ) { int cntPairs = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] ++ ; } else { mp [ arr [ i ] ] = 1 ; } } foreach ( KeyValuePair < int , int > element in mp ) { int i = element . Key ; cntPairs += mp [ i ] * ( mp [ i ] - 1 ) / 2 ; } for ( int i = 0 ; i < N ; i ++ ) { int pairs_after_arr_i_removed = cntPairs + 1 - mp [ arr [ i ] ] ; Console . Write ( pairs_after_arr_i_removed + " ▁ " ) ; } return ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 3 , 2 } ; int N = arr . Length ; pairs_after_removing ( arr , N ) ; } }