# Remove odd frequency characters from the string

Given string str of size N, the task is to remove all the characters from the string that have odd frequencies.

Examples:

Input: str = “geeksforgeeks”
Output: geeksgeeks
The characters f, o, r have odd frequencies
So, they are removed from the string.

Input: str = “zzzxxweeerr”
Output: xxrr

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Create a map and store the frequency of each character from the string to the same map.
• Then, traverse the string and find out which characters have odd frequencies with the help of the map.
• Ignore all those characters which have odd frequencies and store the rest in a new string.
• Finally, display the new string.

Below is the implementation of the above approach:

## C++

 `// C++ program to remove the characters ` `// having odd frequencies in the string ` `#include ` `using` `namespace` `std; ` ` `  `// Function to remove the characters which ` `// have odd frequencies in the string ` `string removeOddFrequencyCharacters(string s) ` `{ ` `    ``// Create a map to store the ` `    ``// frequency of each character ` `    ``unordered_map<``char``, ``int``> m; ` `    ``for` `(``int` `i = 0; i < s.length(); i++) { ` `        ``m[s[i]]++; ` `    ``} ` ` `  `    ``// To store the new string ` `    ``string new_string = ``""``; ` ` `  `    ``// Remove the characters which ` `    ``// have odd frequencies ` `    ``for` `(``int` `i = 0; i < s.length(); i++) { ` ` `  `        ``// If the character has ` `        ``// odd frequency then skip ` `        ``if` `(m[s[i]] & 1) ` `            ``continue``; ` ` `  `        ``// Else concatenate the ` `        ``// character to the new string ` `        ``new_string += s[i]; ` `    ``} ` ` `  `    ``// Return the modified string ` `    ``return` `new_string; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"geeksforgeeks"``; ` ` `  `    ``// Remove the characters which ` `    ``// have odd frequencies ` `    ``str = removeOddFrequencyCharacters(str); ` `    ``cout << str << ``"\n"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to remove the characters ` `// having odd frequencies in the string ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` `      `  `    ``// Function to remove the characters which ` `    ``// have odd frequencies in the string ` `    ``static` `String removeOddFrequencyCharacters(String s) ` `    ``{ ` `        ``// Create a map to store the ` `        ``// frequency of each character ` `        ``HashMap m = ``new` `HashMap(); ` `        ``for` `(``int` `i = ``0``; i < s.length(); i++) { ` `            ``char` `p = s.charAt(i); ` `            ``Integer count = m.get(p); ` `            ``if``( count == ``null``) ` `            ``{ ` `                ``count=``0``; ` `                ``m.put(p,``1``); ` `            ``} ` `            ``else`  `                ``m.put(p,count + ``1``); ` `        ``} ` `      `  `        ``// To store the new string ` `        ``String new_string = ``""``; ` `      `  `        ``// Remove the characters which ` `        ``// have odd frequencies ` `        ``for` `(``int` `i = ``0``; i < s.length(); i++) { ` `      `  `            ``// If the character has ` `            ``// odd frequency then skip ` `            ``if` `((m.get(s.charAt(i))& ``1``)==``1``) ` `                ``continue``; ` `      `  `            ``// Else concatenate the ` `            ``// character to the new string ` `            ``new_string += s.charAt(i); ` `        ``} ` `      `  `        ``// Return the modified string ` `        ``return` `new_string; ` `    ``} ` `      `  `    ``// Driver code ` `    ``public` `static` `void` `main(String []args) ` `    ``{ ` `        ``String str = ``"geeksforgeeks"``; ` `      `  `        ``// Remove the characters which ` `        ``// have odd frequencies ` `        ``str = removeOddFrequencyCharacters(str); ` `        ``System.out.print(str); ` `    ``} ` `} ` ` `  `// This is contributed by chitranayal `

## Python3

 `# Python3 program to remove the characters ` `# having odd frequencies in the string ` ` `  `# Function to remove the characters which ` `# have odd frequencies in the string ` `def` `removeOddFrequencyCharacters(s): ` `     `  `    ``# Create a map to store the ` `    ``# frequency of each character ` `    ``m ``=` `dict``() ` `    ``for` `i ``in` `s: ` `        ``m[i] ``=` `m.get(i, ``0``) ``+` `1` ` `  `    ``# To store the new string ` `    ``new_s ``=` `"" ` ` `  `    ``# Remove the characters which ` `    ``# have odd frequencies ` `    ``for` `i ``in` `s: ` ` `  `        ``# If the character has ` `        ``# odd frequency then skip ` `        ``if` `(m[i] & ``1``): ` `            ``continue` ` `  `        ``# Else concatenate the ` `        ``# character to the new string ` `        ``new_s ``+``=` `i ` ` `  `    ``# Return the modified string ` `    ``return` `new_s ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``str` `=` `"geeksforgeeks"` ` `  `    ``# Remove the characters which ` `    ``# have odd frequencies ` `    ``str` `=` `removeOddFrequencyCharacters(``str``) ` `    ``print``(``str``) ` ` `  `# This code is contributed by mohit kumar 29 `

Output:

```geeksgeeks
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.