Remove characters from string that appears strictly less than K times | Javascript program to reduce the string by removing the characters which appears less than k times ; Function to reduce the string by removing the characters which appears less than k times ; Hash table initialised to 0 ; Increment the frequency of the character ; create a new empty string ; Append the characters which appears more than equal to k times ; Driver Code
let MAX_CHAR = 26 ; function removeChars ( str , k ) { let hash = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) hash [ i ] = 0 ; let n = str . length ; for ( let i = 0 ; i < n ; ++ i ) { hash [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } let res = " " ; for ( let i = 0 ; i < n ; ++ i ) { if ( hash [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] >= k ) { res += str [ i ] ; } } return res ; } let str = " " ; let k = 2 ; document . write ( removeChars ( str , k ) ) ;