Print characters and their frequencies in order of occurrence | Java implementation to print the characters and frequencies in order of its occurrence ; Store all characters and their frequencies in dictionary ; Print characters and their frequencies in same order of their appearance ; Print only if this character is not printed before ; Driver code
import java . util . * ; class Gfg { public static void prCharWithFreq ( String s ) { Map < Character , Integer > d = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( d . containsKey ( s . charAt ( i ) ) ) { d . put ( s . charAt ( i ) , d . get ( s . charAt ( i ) ) + 1 ) ; } else { d . put ( s . charAt ( i ) , 1 ) ; } } for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( d . get ( s . charAt ( i ) ) != 0 ) { System . out . print ( s . charAt ( i ) ) ; System . out . print ( d . get ( s . charAt ( i ) ) + " ▁ " ) ; d . put ( s . charAt ( i ) , 0 ) ; } } } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; prCharWithFreq ( S ) ; } }