Python Program to find Least Frequent Character in String
In this tutorial, you will learn to find the least frequent character in the string. Strings in Python are a sequence of characters wrapped inside single, double, or triple quotes. The frequency of a character is the total number of times that character occurs in the given string. We have to find and print the character which occurs the least in the string in our program.
Look at the examples to understand the input and output format.
Input: "apghphgg"
Output: a
Input: "hjkkjht"
Output: t
To execute this task we can follow multiple approaches, some are discussed below:
- By using keys in the dictionary and min() method
- By using Counter() and min() method
We will discuss all three approaches in detail separately
Approach 1: Keys in dictionary and min()
In this approach, we will iterate through the string and form keys of the characters which occur in the string. The keys will have the value of how many times that character has occurred in the string. Then, using the min() method, we can find the key with the minimum count value, which will be then used to print the required result.
min() method is a function that returns the item with the lowest value.
Algorithm
Step 1- Define a string with characters
Step 2- Declare an empty dictionary
Step 3- Run a loop to iterate through the string
Step 4- Add the character in the dictionary and keep a count of the frequency
Step 5- To get the minimum count use min() and store the value returned by it in a variable
Step 6- Print the variable which has the highest count as the result
Python Program 1
In this program, we have defined a string with certain values and printed the least frequent character in that string using a dictionary char_freq which will store the characters and their occurrences as the key and its value. We can get the Key using get() method.
string= "hgfjiufguij"
print(string)
char_freq={}
for i in string:
if i in char_freq:
char_freq[i]=char_freq[i]+1
else:
char_freq[i] = 1
result= min(char_freq, key = char_freq.get)
print("Least frequent character: ",result)
hgfjiufguij
Least frequent character: h
Approach 2: Counter and min() function
The counter will get the frequency of every character in the string then we can use the min() method to get the minimum count value.
The counter is a sub-class that counts all the hashable objects.
Algorithm
Step 1- Import Counter
Step 2- Declare a string with characters
Step 3- Print string
Step 4- Call the Counter() and pass the string
Step 5- To get the minimum count use min() and store the value returned by it in a variable
Step 6- Print the variable as the result
Python Program 2
To use Counter, we have to import it from the collections class in our program. Since Counter works for hashable objects they are accessed using a key. We can get the Key using get() method.
# least frequent character
from collections import Counter
string= "pppppghhhijeuupffe"
print(string)
# counts hashable objects
result= Counter(string)
# gets minimum out of all values
result= min(result, key=result.get)
print("Least frequent character: ",result)
pppppghhhijeuupffe
Least frequent character: g
Conclusion
In this tutorial, we have seen two approaches to get the least frequent character from the string. The first approach was to find the minimum count using keys of a dictionary and using the key to print the character. The second approach was to use the Counter() method to get the total count for all the characters in the string. Using the min() method we can get the lowest value of count.