Remove characters from a String that appears exactly K times | Javascript program to remove characters from a String that appears exactly K times ; Function to reduce the String by removing the characters which appears exactly k times ; Hash table initialised to 0 ; Increment the frequency of the character ; To store answer ; Next index in reduced string ; Append the characters which appears exactly k times ; Driver code ; Function call
let MAX_CHAR = 26 ; function removeChars ( arr , k ) { let hash = Array . from ( { length : MAX_CHAR } , ( _ , i ) => 0 ) ; let n = arr . length ; for ( let i = 0 ; i < n ; ++ i ) hash [ arr [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; let ans = " " ; for ( let i = 0 ; i < n ; ++ i ) { if ( hash [ arr [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] != k ) { ans += arr [ i ] ; } } return ans ; } let str = " " . split ( ' ' ) ; let k = 2 ; document . write ( removeChars ( str , k ) ) ;