Remove characters from given string whose frequencies are a Prime Number | Function to perform the seive of eratosthenes algorithm ; Initialize all entries in prime [ ] as true ; Initialize 0 and 1 as non prime ; Traversing the prime array ; If i is prime ; All multiples of i must be marked false as they are non prime ; Function to remove characters which have prime frequency in the string ; Length of the string ; Create a boolean array prime ; Sieve of Eratosthenes ; Stores the frequency of character ; Storing the frequencies ; New string that will be formed ; Removing the characters which have prime frequencies ; If the character has prime frequency then skip ; Else concatenate the character to the new string ; Print the modified string ; Driver Code ; Function Call
function SieveOfEratosthenes ( prime , n ) { for ( let i = 0 ; i <= n ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( let i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] == true ) { for ( let j = 2 ; i * j <= n ; j ++ ) { prime [ i * j ] = false ; } } } } function removePrimeFrequencies ( s ) { var n = s . length ; var prime = new Array ( n + 1 ) ; SieveOfEratosthenes ( prime , n ) ; var m = { } ; for ( let i = 0 ; i < s . length ; i ++ ) m [ s [ i ] ] = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { m [ s [ i ] ] ++ ; } var new_string = " " ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( prime [ m [ s [ i ] ] ] ) continue ; new_string += s [ i ] ; } console . log ( new_string ) ; } str = " " ; removePrimeFrequencies ( str ) ;