Print characters having even frequencies in order of occurrence
Last Updated :
16 Mar, 2021
Given a string str containing only lowercase characters. The task is to print the characters having an even frequency in the order of their occurrence.
Note: Repeated elements with even frequency are printed as many times they occur in order of their occurrences.
Examples:
Input: str = “geeksforgeeks”
Output: geeksgeeks
Character |
Frequency |
‘g’ |
2 |
‘e’ |
4 |
‘k’ |
2 |
‘s’ |
2 |
‘f’ |
1 |
‘o’ |
1 |
‘r’ |
1 |
‘g’, ‘e’, ‘k’ and ‘s’ are the only characters with even frequencies.
Input: str = “aeroplane”
Output: aeae
Approach: Create a frequency array to store the frequency of each of the character of the given string str. Traverse the string str again and check whether the frequency of that character is even. If yes, then print the character.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define SIZE 26
void printChar(string str, int n)
{
int freq[SIZE];
memset (freq, 0, sizeof (freq));
for ( int i = 0; i < n; i++)
freq[str[i] - 'a' ]++;
for ( int i = 0; i < n; i++) {
if (freq[str[i] - 'a' ] % 2 == 0) {
cout << str[i];
}
}
}
int main()
{
string str = "geeksforgeeks" ;
int n = str.length();
printChar(str, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int SIZE = 26 ;
static void printChar(String str, int n)
{
int []freq = new int [SIZE];
for ( int i = 0 ; i < n; i++)
freq[str.charAt(i) - 'a' ]++;
for ( int i = 0 ; i < n; i++)
{
if (freq[str.charAt(i) - 'a' ] % 2 == 0 )
{
System.out.print(str.charAt(i));
}
}
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
int n = str.length();
printChar(str, n);
}
}
|
Python3
SIZE = 26
def printChar(string, n):
freq = [ 0 ] * SIZE
for i in range ( 0 , n):
freq[ ord (string[i]) - ord ( 'a' )] + = 1
for i in range ( 0 , n):
if (freq[ ord (string[i]) -
ord ( 'a' )] % 2 = = 0 ):
print (string[i], end = "")
if __name__ = = '__main__' :
string = "geeksforgeeks"
n = len (string)
printChar(string, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int SIZE = 26;
static void printChar(String str, int n)
{
int []freq = new int [SIZE];
for ( int i = 0; i < n; i++)
freq[str[i] - 'a' ]++;
for ( int i = 0; i < n; i++)
{
if (freq[str[i] - 'a' ] % 2 == 0)
{
Console.Write(str[i]);
}
}
}
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
int n = str.Length;
printChar(str, n);
}
}
|
Javascript
<script>
let SIZE = 26;
function printChar(str, n)
{
let freq = new Array(SIZE);
for (let i = 0; i < freq.length; i++)
{
freq[i] = 0;
}
for (let i = 0; i < n; i++)
{
freq[str.charCodeAt(i) - 'a' .charCodeAt(0)]++;
}
for (let i = 0; i < n; i++) {
if (freq[str.charCodeAt(i) - 'a' .charCodeAt(0)] % 2 == 0) {
document.write(str[i]);
}
}
}
let str = "geeksforgeeks" ;
let n = str.length;
printChar(str, n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...