Program to find the least frequent character in the given string using Python

Introduction

In this section of python programming, our task is to find the character having minimum occurrence in the given string.

Program to find the least frequent character in the given string

Program

from collections import Counter
input_str = input("Enter the string:")
# Using Counter() + min()
output = Counter(input_str)
# Find min
output = min(output, key = output.get)
print("The least frequent character is : "+str(output))

Output

Program to find the least frequent character in the given string Output

Explanation

In the above python code, we have used Counter() on the input string. The Counter() function makes the key, value pair of the characters in the input string. To find the minimum frequency character we have used min() function. And the output is printed on the screen.

Form the output above we can see that the characters t, I, n and s are also having least frequency but only character e is printed. We can conclude that the character occurring first is printed only among all the least frequent character.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.