In the previous article, we have discussed Python Program to Find the Sum of all Even Occurring Elements in an Array/List
Given a string and the task is to remove all the even occurring characters from the given string.
Examples:
Example1:
Input:
Given String = "zzzkkooopppzytuh"
Output:
The given string { zzzkkooopppzytuh } after removing even frequency elements is : ooopppytuhExample2:
Input:
Given String = "hellobtechgeeks"
Output:
The given string { hellobtechgeeks } after removing even frequency elements is : obtcgksProgram to Remove Even Frequency Characters from the String in Python
Below are the ways to remove all the even occurring characters from the given string in python:
- Using Dictionary (Hashing, User Input)
- Using Counter() function (Hashing, Static Input)
- Using Counter() function (Hashing, User Input)
- Using Counter() function (Hashing, User Input)
Method #1: Using Dictionary (Hashing, Static Input)
Approach:
- Take a dictionary and initialize it to empty using the {} or dict() say freqncyDictionary.
- Give the string as static input and store it in a variable.
- Loop in the given string using the For loop.
- Inside the For loop, Check if the string character is present in the dictionary or not using the if conditional statement and ‘in’ keyword.
- If it is true then increment the count of the string character in the dictionary by 1.
- Else initialize the dictionary with the string character as key and value as 1.
- Take a string that stores all the characters which are not occurring even a number of times and initialize it to null string using “or str()
- loop in the given string using the for loop.
- Check if the character has an odd frequency by checking the value of that character in the frequency dictionary we check using the if conditional statement.
- If it is true then concatenate this character to modifd_string using string concatenation.
- Print the modifd_string string.
- The Exit of the Program.
Below is the implementation:
# Take a dictionary and initialize it to empty
# using the {} or dict() say freqncyDictionary.
freqncyDictionary = {}
# Give the string as static input and store it in a variable
gvnstrng = "zzzkkooopppzytuh"
# Loop in the given string using the For loop.
for i in gvnstrng:
# Inside the For loop,
# Check if the string character is present in the dictionary
# or not using the if conditional statement and 'in' keyword.
if i in freqncyDictionary.keys():
# If it is true then increment the count of the string character
# in the dictionary by 1.
freqncyDictionary[i] = freqncyDictionary[i]+1
# Else initialize the dictionary with the string character as key and value as 1.
else:
freqncyDictionary[i] = 1
# Take a string which stores all the characters which are not occuring even number
# of times and initialize it to null string using "" or str()
modifd_string = ""
# loop in the given string using the for loop
for charac in gvnstrng:
# check if the character has odd frequency by checking value of that character in frequency dictionary
# we check using the if conditional statement
if(freqncyDictionary[charac] % 2 != 0):
# if it is true then concatenate this character to modifd_string using string concatenation
modifd_string = modifd_string+charac
# print the modifd_string string
print('The given string {', gvnstrng,
'} after removing even frequency elements is :', modifd_string)
Output:
The given string { zzzkkooopppzytuh } after removing even frequency elements is : ooopppytuhMethod #2: Using Dictionary (Hashing, User Input)
Approach:
- Take a dictionary and initialize it to empty using the {} or dict() say freqncyDictionary.
- Give the string as the user input using the input() function and store it in a variable.
- Loop in the given string using the For loop.
- Inside the For loop, Check if the string character is present in the dictionary or not using the if conditional statement and ‘in’ keyword.
- If it is true then increment the count of the string character in the dictionary by 1.
- Else initialize the dictionary with the string character as key and value as 1.
- Take a string that stores all the characters which are not occurring even a number of times and initialize it to null string using “or str()
- loop in the given string using the for loop.
- Check if the character has an odd frequency by checking the value of that character in the frequency dictionary we check using the if conditional statement.
- If it is true then concatenate this character to modifd_string using string concatenation.
- Print the modifd_string string.
- The Exit of the Program.
Below is the implementation:
# Take a dictionary and initialize it to empty
# using the {} or dict() say freqncyDictionary.
freqncyDictionary = {}
# Give the string as the user input using the input() function and store it in a variable.
gvnstrng = input("Enter some random string = ")
# Loop in the given string using the For loop.
for i in gvnstrng:
# Inside the For loop,
# Check if the string character is present in the dictionary
# or not using the if conditional statement and 'in' keyword.
if i in freqncyDictionary.keys():
# If it is true then increment the count of the string character
# in the dictionary by 1.
freqncyDictionary[i] = freqncyDictionary[i]+1
# Else initialize the dictionary with the string character as key and value as 1.
else:
freqncyDictionary[i] = 1
# Take a string which stores all the characters which are not occuring even number
# of times and initialize it to null string using "" or str()
modifd_string = ""
# loop in the given string using the for loop
for charac in gvnstrng:
# check if the character has odd frequency by checking value of that character in frequency dictionary
# we check using the if conditional statement
if(freqncyDictionary[charac] % 2 != 0):
# if it is true then concatenate this character to modifd_string using string concatenation
modifd_string = modifd_string+charac
# print the modifd_string string
print('The given string {', gvnstrng,
'} after removing even frequency elements is :', modifd_string)Output:
Enter some random string = hellobtechgeeks
The given string { hellobtechgeeks } after removing even frequency elements is : obtcgksMethod #3: Using Counter() function (Hashing, Static Input)
Approach:
- Import the Counter() function from collections using the import keyword.
- Give the string as static input and store it in a variable
- Calculate the frequency of all the given string characters using the Counter() function which returns the element and its frequency as key-value pair  and store this dictionary in a variable(say freqncyDictionary)
- Take a string that stores all the characters which are not occurring even a number of times and initialize it to null string using “or str()
- loop in the given string using the for loop.
- Check if the character has an odd frequency by checking the value of that character in the frequency dictionary we check using the if conditional statement.
- If it is true then concatenate this character to modifd_string using string concatenation.
- Print the modifd_string string.
- The Exit of the Program.
Below is the implementation:
# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the string as static input and store it in a variable
gvnstrng = "zzzkkooopppzytuh"
# Calculate the frequency of all the given string characters using the Counter()
# function which returns the element and its frequency as key-value pair
# and store this dictionary in a variable(say freqncyDictionary)
freqncyDictionary = Counter(gvnstrng)
# Take a string which stores all the characters which are not occuring even number
# of times and initialize it to null string using "" or str()
modifd_string = ""
# loop in the given string using the for loop
for charac in gvnstrng:
# check if the character has odd frequency by checking value of that character in frequency dictionary
# we check using the if conditional statement
if(freqncyDictionary[charac] % 2 != 0):
# if it is true then concatenate this character to modifd_string using string concatenation
modifd_string = modifd_string+charac
# print the modifd_string string
print('The given string {', gvnstrng,
'} after removing even frequency elements is :', modifd_string)Output:
The given string { zzzkkooopppzytuh } after removing even frequency elements is : ooopppytuhMethod #4: Using Counter() function (Hashing, User Input)
Approach:
- Import the Counter() function from collections using the import keyword.
- Give the string as the user input using the input() function and store it in a variable.
- Calculate the frequency of all the given string characters using the Counter() function which returns the element and its frequency as key-value pair  and store this dictionary in a variable(say freqncyDictionary)
- Take a string that stores all the characters which are not occurring even a number of times and initialize it to null string using “or str()
- loop in the given string using the for loop.
- Check if the character has an odd frequency by checking the value of that character in the frequency dictionary we check using the if conditional statement.
- If it is true then concatenate this character to modifd_string using string concatenation.
- Print the modifd_string string.
- The Exit of the Program.
Below is the implementation:
# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the string as user input using the input() function and store it in a variable
gvnstrng = input("Enter some random string = ")
# Calculate the frequency of all the given string characters using the Counter()
# function which returns the element and its frequency as key-value pair
# and store this dictionary in a variable(say freqncyDictionary)
freqncyDictionary = Counter(gvnstrng)
# Take a string which stores all the characters which are not occuring even number
# of times and initialize it to null string using "" or str()
modifd_string = ""
# loop in the given string using the for loop
for charac in gvnstrng:
# check if the character has odd frequency by checking value of that character in frequency dictionary
# we check using the if conditional statement
if(freqncyDictionary[charac] % 2 != 0):
# if it is true then concatenate this character to modifd_string using string concatenation
modifd_string = modifd_string+charac
# print the modifd_string string
print('The given string {', gvnstrng,
'} after removing even frequency elements is :', modifd_string)Output:
Enter some random string = hellobtechgeeks
The given string { hellobtechgeeks } after removing even frequency elements is : obtcgksExplore more Example Python Programs with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.
- Python Program to Find the Sum of all Odd Frequency Elements in an Array/List
- Python Program to Remove Odd Occurring Characters from the String
- Python Program to Find the sum of all Highest Occurring Elements in an Array/List
- Python Program to Find the Sum of all Even Occurring Elements in an Array/List
