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 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’ll get a Counter class object with characters as keys and their frequency in a string as values.
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’ll use the [] operator to get the occurrence count of the characters from it.
Examples:
Example1:
Input:
Given String = "hello this is btechgeeks"
Output:
In a Given String,{ hello this is btechgeeks } all Non-repeating Characters are:
o b c g kExample2:
Input:
Given String = "good morning btechgeeks"
Output:
In a Given String,{ good morning btechgeeks } all Non-repeating Characters are:
d m r i b t c h k sProgram to Find All Non-Repeated Characters in a String in Python
Below are the ways to find all the Non repeated characters in a given String:
Method #1: 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 elements using the Counter() function which returns the element and its frequency as a key-value pair and stores this dictionary in another variable (say strngfreqelements).
- Traverse in this frequency dictionary using the for loop.
- Inside the loop, check if the Key has the frequency 1 and key not equal to space using the if conditional statement
- If the statement is true, then print the value of the key.
- 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.
gven_str = "hello this is btechgeeks"
# Calculate the frequency of all the given string elements using the Counter() function
# which returns the element and its frequency as a key-value pair and stores this
# dictionary in another variable (say strngfreqelements).
strngfreqelements = Counter(gven_str)
print("In a Given String,{", gven_str, "} all Non-repeating Characters are:")
# Traverse in this frequency dictionary using the for loop.
for key in strngfreqelements:
# Inside the loop, check if the Key has the frequency 1 and key not equal to space
# using the if conditional statement
if(strngfreqelements[key] == 1 and key != " "):
# If the statement is true, then print the value of the key.
print(key, end=" ")
Output:
In a Given String,{ hello this is btechgeeks } all Non-repeating Characters are:
o b c g kMethod #2: Using Counter() function (Hashing, User Input)
Approach:
- Import the Counter() function from collections using the import keyword.
- Give the string as user input using the input() function and store it in a variable.
- Calculate the frequency of all the given string elements using the Counter() function which returns the element and its frequency as a key-value pair and stores this dictionary in another variable (say strngfreqelements).
- Traverse in this frequency dictionary using the for loop.
- Inside the loop, check if the Key has the frequency 1 and key not equal to space using the if conditional statement
- If the statement is true, then print the value of the key.
- 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.
gven_str = input("Enter some random String = ")
# Calculate the frequency of all the given string elements using the Counter() function
# which returns the element and its frequency as a key-value pair and stores this
# dictionary in another variable (say strngfreqelements).
strngfreqelements = Counter(gven_str)
print("In a Given String,{", gven_str, "} all Non-repeating Characters are:")
# Traverse in this frequency dictionary using the for loop.
for key in strngfreqelements:
# Inside the loop, check if the Key has the frequency 1 and key not equal to space
# using the if conditional statement
if(strngfreqelements[key] == 1 and key != " "):
# If the statement is true, then print the value of the key.
print(key, end=" ")
Output:
Enter some random String = good morning btechgeeks
In a Given String,{ good morning btechgeeks } all Non-repeating Characters are:
d m r i b t c h k sFind a comprehensive collection of Examples of Python Programs ranging from simple ones to complex ones to guide you throughout your coding journey.
