Map to store the frequency of each element
HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < 2 * n ; i ++ ) { if ( mp . get ( a [ i ] ) == null ) mp . put ( a [ i ] , 1 ) ; else mp . put ( a [ i ] , mp . get ( a [ i ] ) + 1 ) ;