Building HashMap to store frequencies of all characters .
HashMap < Character , Integer > hm = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < temp . length ; i ++ ) { if ( hm . containsKey ( temp [ i ] ) ) hm . put ( temp [ i ] , hm . get ( temp [ i ] ) + 1 ) ; else hm . put ( temp [ i ] , 1 ) ; }