{"id":20312,"date":"2021-09-09T19:57:58","date_gmt":"2021-09-09T14:27:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=20312"},"modified":"2021-11-22T18:36:22","modified_gmt":"2021-11-22T13:06:22","slug":"python-program-to-find-all-non-repeated-characters-in-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-all-non-repeated-characters-in-a-string\/","title":{"rendered":"Python Program to Find All Non Repeated Characters in a String"},"content":{"rendered":"
In the previous article, we have discussed Program to Find Lexicographic Rank of a Given String<\/a><\/p>\n Given a string and the task is to find all the Non repeated characters in a given String.<\/p>\n Counter function in Python:<\/strong><\/p>\n The counter is a set and dict subset. Counter() takes an iterable entity as an argument and stores the elements as keys and the frequency of the elements as a value. So, in collections, if we transfer a string. When you call Counter(), you\u2019ll get a Counter class object with characters as keys and their frequency in a string as values.<\/p>\n Counter() returns a Counter type object (a subclass of dict) with all characters in the string as keys and their occurrence count as values. We\u2019ll use the [] operator to get the occurrence count of the characters from it.<\/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 find all the Non repeated characters in a given String:<\/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 Find a comprehensive collection of Examples of Python Programs<\/a> ranging from simple ones to complex ones to guide you throughout your coding journey.<\/p>\n In the previous article, we have discussed Program to Find Lexicographic Rank of a Given String Given a string and the task is to find all the Non repeated characters in a given String. Counter function in Python: The counter is a set and dict subset. Counter() takes an iterable entity as an argument and …<\/p>\nGiven String = \"hello this is btechgeeks\"<\/pre>\n
In a Given String,{ hello this is btechgeeks } all Non-repeating Characters are:\r\no b c g k<\/pre>\n
Given String = \"good morning btechgeeks\"<\/pre>\n
In a Given String,{ good morning btechgeeks } all Non-repeating Characters are:\r\nd m r i b t c h k s<\/pre>\n
Program to Find All Non-Repeated Characters in a String in Python<\/h2>\n
\n
Method #1: 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\ngven_str = \"hello this is btechgeeks\"\r\n# Calculate the frequency of all the given string elements using the Counter() function\r\n# which returns the element and its frequency as a key-value pair and stores this\r\n# dictionary in another variable (say\u00a0strngfreqelements).\r\nstrngfreqelements = Counter(gven_str)\r\nprint(\"In a Given String,{\", gven_str, \"} all Non-repeating Characters are:\")\r\n# Traverse in this frequency dictionary using the for loop.\r\nfor key in strngfreqelements:\r\n # Inside the loop, check if the Key has the frequency 1 and key not equal to space\r\n # using the if conditional statement\r\n if(strngfreqelements[key] == 1 and key != \" \"):\r\n # If the statement is true, then print the value of the key.\r\n print(key, end=\" \")\r\n<\/pre>\n
In a Given String,{ hello this is btechgeeks } all Non-repeating Characters are:\r\no b c g k<\/pre>\n
Method #2: 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 \r\n# store it in a variable.\r\ngven_str = input(\"Enter some random String = \")\r\n# Calculate the frequency of all the given string elements using the Counter() function\r\n# which returns the element and its frequency as a key-value pair and stores this\r\n# dictionary in another variable (say\u00a0strngfreqelements).\r\nstrngfreqelements = Counter(gven_str)\r\nprint(\"In a Given String,{\", gven_str, \"} all Non-repeating Characters are:\")\r\n# Traverse in this frequency dictionary using the for loop.\r\nfor key in strngfreqelements:\r\n # Inside the loop, check if the Key has the frequency 1 and key not equal to space\r\n # using the if conditional statement\r\n if(strngfreqelements[key] == 1 and key != \" \"):\r\n # If the statement is true, then print the value of the key.\r\n print(key, end=\" \")\r\n<\/pre>\n
Enter some random String = good morning btechgeeks\r\nIn a Given String,{ good morning btechgeeks } all Non-repeating Characters are:\r\nd m r i b t c h k s<\/pre>\n
\n