Count pairs of equal array elements remaining after every removal | Java 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
import java . util . * ; class GFG { static void pairs_after_removing ( int arr [ ] , int N ) { int cntPairs = 0 ; Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { mp . put ( arr [ i ] , mp . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; } for ( Map . Entry < Integer , Integer > element : mp . entrySet ( ) ) { int i = element . getKey ( ) ; cntPairs += mp . get ( i ) * ( mp . get ( i ) - 1 ) / 2 ; } for ( int i = 0 ; i < N ; i ++ ) { int pairs_after_arr_i_removed = cntPairs + 1 - mp . get ( arr [ i ] ) ; System . out . print ( pairs_after_arr_i_removed + " ▁ " ) ; } return ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 3 , 2 } ; int N = arr . length ; pairs_after_removing ( arr , N ) ; } }