In the previous article, we have discussed Python Program for Given Two Numbers a and b Find all x Such that a % x = b
Given a string and the task is to remove all the odd occurring characters from the given string.
Examples:
Example1:
Input:
Given String = "goodmorning"
Output:
The given string { goodmorning } after removing odd frequency elements is : gnngExample2:
Input:
Given String = "thisisbtechgeeks"
Output:
The given string { thisisbtechgeeks } after removing odd frequency elements is : thiithProgram to Remove Odd Occurring Characters from the String in Python.
Below are the ways to remove all the odd 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 the above condition 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 odd 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 even 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 = "goodmorning"
# 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 odd 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 even 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 odd frequency elements is :', modifd_string)Output:
The given string { goodmorning } after removing odd frequency elements is : gnngMethod #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 the above condition 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 odd 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 even 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 odd 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 even 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 odd frequency elements is :', modifd_string)Output:
Enter some random string = thisisbtechgeeks
The given string { thisisbtechgeeks } after removing odd frequency elements is : thiithMethod #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 odd 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 even frequency by checking the value of that character in the frequency dictionary we check using the if conditional statement.
- If the above condition 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 = "goodmorning"
# 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 odd 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 even 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 odd frequency elements is :', modifd_string)
Output:
The given string { goodmorning } after removing odd frequency elements is : gnngMethod #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 odd 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 even frequency by checking the value of that character in the frequency dictionary we check using the if conditional statement.
- If the above condition 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 odd 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 even 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 odd frequency elements is :', modifd_string)
Output:
Enter some random string = thisisbtechgeeks
The given string { thisisbtechgeeks } after removing odd frequency elements is : thiithRemediate your knowledge gap by attempting the Python Code Examples regularly and understand the areas of need and work on them.
