Saturday, November 16, 2024
Google search engine
HomeData Modelling & AIPrint characters having even frequencies in order of occurrence

Print characters having even frequencies in order of occurrence

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 = “neveropen” 
Output: neveropenneveropen 
 

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++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
#define SIZE 26
 
// Function to print the even frequency characters
// in the order of their occurrence
void printChar(string str, int n)
{
 
    // To store the frequency of each of
    // the character of the string
    int freq[SIZE];
 
    // Initialize all elements of freq[] to 0
    memset(freq, 0, sizeof(freq));
 
    // Update the frequency of each character
    for (int i = 0; i < n; i++)
        freq[str[i] - 'a']++;
 
    // Traverse str character by character
    for (int i = 0; i < n; i++) {
 
        // If frequency of current character is even
        if (freq[str[i] - 'a'] % 2 == 0) {
            cout << str[i];
        }
    }
}
 
// Driver code
int main()
{
    string str = "neveropen";
    int n = str.length();
    printChar(str, n);
 
    return 0;
}


Java




// Java implementation of the approach
import java.util.*;
 
class GFG
{
static int SIZE = 26;
 
// Function to print the even frequency characters
// in the order of their occurrence
static void printChar(String str, int n)
{
 
    // To store the frequency of each of
    // the character of the string
    int []freq = new int[SIZE];
 
    // Update the frequency of each character
    for (int i = 0; i < n; i++)
        freq[str.charAt(i) - 'a']++;
 
    // Traverse str character by character
    for (int i = 0; i < n; i++)
    {
 
        // If frequency of current character is even
        if (freq[str.charAt(i) - 'a'] % 2 == 0)
        {
            System.out.print(str.charAt(i));
        }
    }
}
 
// Driver code
public static void main(String[] args)
{
    String str = "neveropen";
    int n = str.length();
    printChar(str, n);
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python3 implementation of the approach
SIZE = 26
 
# Function to print the even frequency characters
# in the order of their occurrence
def printChar(string, n):
 
    # To store the frequency of each of
    # the character of the stringing
    # Initialize all elements of freq[] to 0
    freq = [0] * SIZE
 
    # Update the frequency of each character
    for i in range(0, n):
        freq[ord(string[i]) - ord('a')] += 1
 
    # Traverse string character by character
    for i in range(0, n):
 
        # If frequency of current character is even
        if (freq[ord(string[i]) -
                 ord('a')] % 2 == 0):
            print(string[i], end = "")
         
# Driver code
if __name__ == '__main__':
    string = "neveropen"
    n = len(string)
    printChar(string, n)
 
# This code is contributed by Ashutosh450


C#




// C# implementation of the approach
using System;
using System.Collections.Generic;
     
class GFG
{
static int SIZE = 26;
 
// Function to print the even frequency characters
// in the order of their occurrence
static void printChar(String str, int n)
{
 
    // To store the frequency of each of
    // the character of the string
    int []freq = new int[SIZE];
 
    // Update the frequency of each character
    for (int i = 0; i < n; i++)
        freq[str[i] - 'a']++;
 
    // Traverse str character by character
    for (int i = 0; i < n; i++)
    {
 
        // If frequency of current character is even
        if (freq[str[i] - 'a'] % 2 == 0)
        {
            Console.Write(str[i]);
        }
    }
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "neveropen";
    int n = str.Length;
    printChar(str, n);
}
}
 
// This code is contributed by Princi Singh


Javascript




<script>
 
    // Javascript implementation of the approach
     
    let SIZE = 26;
     
    // Function to print the even frequency characters
    // in the order of their occurrence
    function printChar(str, n)
    {
        // To store the frequency of each of
        // the character of the string
        let freq = new Array(SIZE);
       
        // Initialize all elements of freq[] to 0
        for(let i = 0; i < freq.length; i++)
        {
            freq[i] = 0;
        }
       
        // Update the frequency of each character
        for (let i = 0; i < n; i++)
        {
            freq[str.charCodeAt(i) - 'a'.charCodeAt(0)]++;
        }
             
        // Traverse str character by character
        for (let i = 0; i < n; i++) {
       
            // If frequency of current character is even
            if (freq[str.charCodeAt(i) - 'a'.charCodeAt(0)] % 2 == 0) {
                document.write(str[i]);
            }
        }
    }
 
     
    let str = "neveropen";
    let n = str.length;
    printChar(str, n);
 
</script>


Output: 

neveropenneveropen

 

Time Complexity: O(n) 
Auxiliary Space: O(1)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments