Remove characters from string that appears strictly less than K times | C # 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
using System ; class GFG { readonly static int MAX_CHAR = 26 ; static String removeChars ( String str , int k ) { int [ ] hash = new int [ MAX_CHAR ] ; int n = str . Length ; for ( int i = 0 ; i < n ; ++ i ) { hash [ str [ i ] - ' a ' ] ++ ; } String res = " " ; for ( int i = 0 ; i < n ; ++ i ) { if ( hash [ str [ i ] - ' a ' ] >= k ) { res += str [ i ] ; } } return res ; } static public void Main ( ) { String str = " geeksforgeeks " ; int k = 2 ; Console . WriteLine ( removeChars ( str , k ) ) ; } }