{"id":20620,"date":"2021-09-21T08:59:24","date_gmt":"2021-09-21T03:29:24","guid":{"rendered":"https:\/\/python-programs.com\/?p=20620"},"modified":"2021-11-22T18:36:17","modified_gmt":"2021-11-22T13:06:17","slug":"python-program-to-remove-even-frequency-characters-from-the-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-remove-even-frequency-characters-from-the-string\/","title":{"rendered":"Python Program to Remove Even Frequency Characters from the String"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find the Sum of all Even Occurring Elements in an Array\/List<\/a><\/p>\n Given a string and the task is to remove all the even occurring characters from the given string.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to remove all the even occurring characters from the given string in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more Example Python Programs<\/a> with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.<\/p>\n 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 …<\/p>\nGiven String = \"zzzkkooopppzytuh\"<\/pre>\n
The given string { zzzkkooopppzytuh } after removing even frequency elements is : ooopppytuh<\/pre>\n
Given String = \"hellobtechgeeks\"<\/pre>\n
The given string { hellobtechgeeks } after removing even frequency elements is : obtcgks<\/pre>\n
Program to Remove Even Frequency Characters from the String in Python<\/h2>\n
\n
Method #1: Using Dictionary (Hashing, Static Input)<\/h3>\n
\n
# Take a dictionary and initialize it to empty\r\n# using the {} or dict() say freqncyDictionary.\r\nfreqncyDictionary = {}\r\n# Give the string as static input and store it in a variable\r\ngvnstrng = \"zzzkkooopppzytuh\"\r\n# Loop in the given string using the For loop.\r\nfor i in gvnstrng:\r\n # Inside the For loop,\r\n # Check if the string character is present in the dictionary\r\n # or not using the if conditional statement and 'in' keyword.\r\n if i in freqncyDictionary.keys():\r\n # If it is true then increment the count of the string character\r\n # in the dictionary by 1.\r\n freqncyDictionary[i] = freqncyDictionary[i]+1\r\n # Else initialize the dictionary with the string character as key and value as 1.\r\n else:\r\n freqncyDictionary[i] = 1\r\n# Take a string which stores all the characters which are not occuring even number\r\n# of times and initialize it to null string using \"\" or str()\r\nmodifd_string = \"\"\r\n# loop in the given string using the for loop\r\nfor charac in gvnstrng:\r\n\r\n # check if the character has odd frequency by checking value of that character in frequency dictionary\r\n # we check using the if conditional statement\r\n if(freqncyDictionary[charac] % 2 != 0):\r\n # if it is true then concatenate this character to modifd_string using string concatenation\r\n modifd_string = modifd_string+charac\r\n\r\n\r\n# print the modifd_string string\r\nprint('The given string {', gvnstrng,\r\n '} after removing even frequency elements is :', modifd_string)\r\n<\/pre>\n
The given string { zzzkkooopppzytuh } after removing even frequency elements is : ooopppytuh<\/pre>\n
Method #2: Using Dictionary (Hashing, User Input)<\/h3>\n
\n
# Take a dictionary and initialize it to empty\r\n# using the {} or dict() say freqncyDictionary.\r\nfreqncyDictionary = {}\r\n# Give the string as the user input using the input() function and store it in a variable.\r\ngvnstrng = input(\"Enter some random string = \")\r\n# Loop in the given string using the For loop.\r\nfor i in gvnstrng:\r\n # Inside the For loop,\r\n # Check if the string character is present in the dictionary\r\n # or not using the if conditional statement and 'in' keyword.\r\n if i in freqncyDictionary.keys():\r\n # If it is true then increment the count of the string character\r\n # in the dictionary by 1.\r\n freqncyDictionary[i] = freqncyDictionary[i]+1\r\n # Else initialize the dictionary with the string character as key and value as 1.\r\n else:\r\n freqncyDictionary[i] = 1\r\n# Take a string which stores all the characters which are not occuring even number\r\n# of times and initialize it to null string using \"\" or str()\r\nmodifd_string = \"\"\r\n# loop in the given string using the for loop\r\nfor charac in gvnstrng:\r\n\r\n # check if the character has odd frequency by checking value of that character in frequency dictionary\r\n # we check using the if conditional statement\r\n if(freqncyDictionary[charac] % 2 != 0):\r\n # if it is true then concatenate this character to modifd_string using string concatenation\r\n modifd_string = modifd_string+charac\r\n\r\n\r\n# print the modifd_string string\r\nprint('The given string {', gvnstrng,\r\n '} after removing even frequency elements is :', modifd_string)<\/pre>\n
Enter some random string = hellobtechgeeks\r\nThe given string { hellobtechgeeks } after removing even frequency elements is : obtcgks<\/pre>\n
Method #3: Using Counter() function (Hashing, Static Input)<\/h3>\n
\n
# Import the Counter() function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the string as static input and store it in a variable\r\ngvnstrng = \"zzzkkooopppzytuh\"\r\n# Calculate the frequency of all the given string characters using the Counter()\r\n# function which returns the element and its frequency as key-value pair\r\n# and store this dictionary in a variable(say freqncyDictionary)\r\nfreqncyDictionary = Counter(gvnstrng)\r\n# Take a string which stores all the characters which are not occuring even number\r\n# of times and initialize it to null string using \"\" or str()\r\nmodifd_string = \"\"\r\n# loop in the given string using the for loop\r\nfor charac in gvnstrng:\r\n\r\n # check if the character has odd frequency by checking value of that character in frequency dictionary\r\n # we check using the if conditional statement\r\n if(freqncyDictionary[charac] % 2 != 0):\r\n # if it is true then concatenate this character to modifd_string using string concatenation\r\n modifd_string = modifd_string+charac\r\n\r\n\r\n# print the modifd_string string\r\nprint('The given string {', gvnstrng,\r\n '} after removing even frequency elements is :', modifd_string)<\/pre>\n
The given string { zzzkkooopppzytuh } after removing even frequency elements is : ooopppytuh<\/pre>\n
Method #4: Using Counter() function (Hashing, User Input)<\/h3>\n
\n
# Import the Counter() function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the string as user input using the input() function and store it in a variable\r\ngvnstrng = input(\"Enter some random string = \")\r\n# Calculate the frequency of all the given string characters using the Counter()\r\n# function which returns the element and its frequency as key-value pair\r\n# and store this dictionary in a variable(say freqncyDictionary)\r\nfreqncyDictionary = Counter(gvnstrng)\r\n# Take a string which stores all the characters which are not occuring even number\r\n# of times and initialize it to null string using \"\" or str()\r\nmodifd_string = \"\"\r\n# loop in the given string using the for loop\r\nfor charac in gvnstrng:\r\n\r\n # check if the character has odd frequency by checking value of that character in frequency dictionary\r\n # we check using the if conditional statement\r\n if(freqncyDictionary[charac] % 2 != 0):\r\n # if it is true then concatenate this character to modifd_string using string concatenation\r\n modifd_string = modifd_string+charac\r\n\r\n\r\n# print the modifd_string string\r\nprint('The given string {', gvnstrng,\r\n '} after removing even frequency elements is :', modifd_string)<\/pre>\n
Enter some random string = hellobtechgeeks\r\nThe given string { hellobtechgeeks } after removing even frequency elements is : obtcgks<\/pre>\n
\n