# Remove characters that appear more than k times

Given a string of lowercase letters, reduce it by removing the characters which appears more than k times in the string.

**Examples:**

Input : str = "geeksforgeeks" k = 2 Output : for Input : str = "geeksforgeeks" k = 3 Output : gksforgks

**Approach :**

- Create a hash table of 26 indexes, where 0th index represent ‘a’ and 1th index represent ‘b’ and so on. Initialise the hash table to zero.
- Iterate through the string and count increment the frequency the str[i] character in the hash table.
- Now once again traverse through the string and append those characters in the new string whose frequency in the hash table is less than k and skip those which appears more than equal to k.

**Time Complexity** – O(N)

Below is the implementation of above approach:

## C++

`// C++ program to reduce the string by ` `// removing the characters which ` `// appears more than k times ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `MAX_CHAR = 26; ` ` ` `void` `removeChars(` `char` `arr[], ` `int` `k) ` `{ ` ` ` `// Hash table initialised to 0 ` ` ` `int` `hash[MAX_CHAR] = { 0 }; ` ` ` ` ` `// Increment the frequency of the character ` ` ` `int` `n = ` `strlen` `(arr); ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `hash[arr[i] - ` `'a'` `]++; ` ` ` ` ` `// Next index in reduced string ` ` ` `int` `index = 0; ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` ` ` `// Append the characters which ` ` ` `// appears less than k times ` ` ` `if` `(hash[arr[i] - ` `'a'` `] < k) { ` ` ` `arr[index++] = arr[i]; ` ` ` `} ` ` ` `} ` ` ` ` ` `arr[index] = ` `'\0'` `; ` `} ` ` ` `int` `main() ` `{ ` ` ` `char` `str[] = ` `"geeksforgeeks"` `; ` ` ` `int` `k = 2; ` ` ` `removeChars(str, k); ` ` ` `cout << str; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to reduce the string by ` `// removing the characters which ` `// appears more than k times ` `import` `java.util.*; ` `class` `Solution ` `{ ` ` ` `static` `final` `int` `MAX_CHAR = ` `26` `; ` ` ` `static` `void` `removeChars(` `char` `arr[], ` `int` `k) ` `{ ` ` ` `// Hash table initialised to 0 ` ` ` `int` `hash[]=` `new` `int` `[MAX_CHAR]; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i <MAX_CHAR; ++i) ` ` ` `hash[i]=` `0` `; ` ` ` ` ` `// Increment the frequency of the character ` ` ` `int` `n = (arr).length; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; ++i) ` ` ` `hash[arr[i] - ` `'a'` `]++; ` ` ` ` ` `// Next index in reduced string ` ` ` `int` `index = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; ++i) { ` ` ` ` ` `// Append the characters which ` ` ` `// appears less than k times ` ` ` `if` `(hash[arr[i] - ` `'a'` `] < k) { ` ` ` `arr[index++] = arr[i]; ` ` ` `} ` ` ` `} ` ` ` `for` `(` `int` `i = index; i < n; ++i) ` ` ` `arr[i] = ` `' '` `; ` `} ` ` ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `char` `str[] = ` `"geeksforgeeks"` `.toCharArray();; ` ` ` `int` `k = ` `2` `; ` ` ` `removeChars(str, k); ` ` ` `System.out.println(String.valueOf( str)); ` `} ` ` ` `} ` `//contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to reduce the string by ` `# removing the characters which ` `# appears more than k times ` `MAX_CHAR ` `=` `26` ` ` `def` `removeChars(arr, k): ` ` ` ` ` `# Hash table initialised to 0 ` ` ` `hash` `=` `[` `0` `for` `i ` `in` `range` `(MAX_CHAR)] ` ` ` ` ` `# Increment the frequency of the character ` ` ` `n ` `=` `len` `(arr) ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `hash` `[` `ord` `(arr[i]) ` `-` `ord` `(` `'a'` `)] ` `+` `=` `1` ` ` ` ` `# Next index in reduced string ` ` ` `index ` `=` `0` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# Append the characters which ` ` ` `# appears less than k times ` ` ` `if` `(` `hash` `[` `ord` `(arr[i]) ` `-` `ord` `(` `'a'` `)] < k): ` ` ` `arr[index] ` `=` `arr[i] ` ` ` `index ` `+` `=` `1` ` ` ` ` `arr[index] ` `=` `' '` ` ` ` ` `for` `i ` `in` `range` `(index): ` ` ` `print` `(arr[i], end ` `=` `'') ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `str` `=` `"geeksforgeeks"` ` ` `str` `=` `list` `(` `str` `) ` ` ` `k ` `=` `2` ` ` `removeChars(` `str` `, k) ` ` ` `# This code is contributed by ` `# Shashank_Sharma ` |

*chevron_right*

*filter_none*

## C#

`// C# program to reduce the string by ` `// removing the characters which ` `// appears more than k times ` `using` `System; ` `public` `class` `Solution{ ` ` ` ` ` `static` `readonly` `int` `MAX_CHAR = 26; ` ` ` ` ` `static` `void` `removeChars(` `char` `[]arr, ` `int` `k) ` ` ` `{ ` ` ` `// Hash table initialised to 0 ` ` ` `int` `[]hash=` `new` `int` `[MAX_CHAR]; ` ` ` ` ` `for` `(` `int` `i = 0; i <MAX_CHAR; ++i) ` ` ` `hash[i]=0; ` ` ` ` ` `// Increment the frequency of the character ` ` ` `int` `n = (arr).Length; ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `hash[arr[i] - ` `'a'` `]++; ` ` ` ` ` `// Next index in reduced string ` ` ` `int` `index = 0; ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` ` ` `// Append the characters which ` ` ` `// appears less than k times ` ` ` `if` `(hash[arr[i] - ` `'a'` `] < k) { ` ` ` `arr[index++] = arr[i]; ` ` ` `} ` ` ` `} ` ` ` `for` `(` `int` `i = index; i < n; ++i) ` ` ` `arr[i] = ` `' '` `; ` ` ` `} ` ` ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `char` `[]str = ` `"geeksforgeeks"` `.ToCharArray();; ` ` ` `int` `k = 2; ` ` ` `removeChars(str, k); ` ` ` `Console.Write(String.Join(` `""` `,str)); ` ` ` `} ` ` ` `} ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

for

## Recommended Posts:

- Remove characters from string that appears strictly less than K times
- Remove elements from the array which appear more than k times
- Remove elements that appear strictly less than k times
- Largest sub-string where all the characters appear at least K times
- Count distinct substrings that contain some characters at most k times
- Remove all characters other than alphabets from string
- Remove even frequency characters from the string
- Minimum characters to be replaced to remove the given substring
- Remove all non-alphabetical characters of a String in Java
- Remove first adjacent pairs of similar characters until possible
- Remove minimum number of characters so that two strings become anagram
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Remove characters from a numeric string such that string becomes divisible by 8
- Remove characters from the first string which are present in the second string
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.