create map of elements of array A1
Map < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n1 ; ++ i ) { if ( m . containsKey ( A1 [ i ] ) ) m . put ( A1 [ i ] , m . get ( A1 [ i ] ) + 1 ) ; else m . put ( A1 [ i ] , 1 ) ; }