Print characters having prime frequencies in order of occurrence | Function to check primes ; Counting the frequency of all character using Counter function ; Traversing string ; Driver code ; Passing string to checkString function
function prime ( n ) { if ( n <= 1 ) return false ; let max_div = Math . floor ( Math . sqrt ( n ) ) ; for ( let i = 2 ; i < 1 + max_div ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } function checkString ( s ) { let freq = new Map ( ) ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( ! freq . has ( s [ i ] ) ) freq . set ( s [ i ] , 0 ) ; freq . set ( s [ i ] , freq . get ( s [ i ] ) + 1 ) ; } for ( let i = 0 ; i < s . length ; i ++ ) { if ( prime ( freq . get ( s [ i ] ) ) ) document . write ( s [ i ] ) ; } } let s = " " ; checkString ( s ) ;