Q:

Python program to find the least frequent character in the string

belongs to collection: Python String Programs

0

Find the least frequent character in a string

We will take the string as input from the user and then find the least frequent character in the string.

Example:

Input: "book:
Output: "b"

To find the least frequent character in the string, we will count the frequency of all characters in the string and then extract the character with the least frequency.

To store characters and their frequencies, we will use a dictionary.

Python provides different methods to find the least frequent character.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Method 1: Using loop and min() method

We will loop over the array and find the frequency of occurrence of characters of the string. Then we will print the character will the least frequency.

Algorithm :

  • Initialize : dictionary freq{}
  • Loop over the string
    • If the character exists, increase its frequency.
    • If the character does not exist in dict., add its frequency to 1.
  • Find the character with least frequency, using min() method.
  • Print the least frequent character.

Program to find the least frequent character in the string

# Python program to find the least frequent character of the string

# Getting string input from the user 
myStr =  input('Enter the string : ')

# Finding the least frequent character of the string 
freq = {}
for i in myStr:
    if i in freq:
        freq[i] += 1
    else:
        freq[i] = 1
leastFreq = min(freq, key = freq.get)

# Printing values 
print("Entered String is ", myStr)
print(leastFreq , "is the least frequent character with frequency of " , freq[leastFreq])

Output:

RUN 1:
Enter the string : Python programming
Entered String is  Python programming
h is the least frequent character with frequency of  1

RUN 2:
Enter the string : AAABBBCDDD
Entered String is  AAABBBCDDD
C is the least frequent character with frequency of  1

Method 2: Using the counter() method

Python provides a method counter() in its collections library which is used to count the frequency of values of a collection.

Then we will find the least frequency using min method.

Algorithm :

  • Initialize : dictionary freq{}
  • Using collections.counter() method, find the frequency of each character.
  • Find the character with least frequency, using min() method.
  • Print the least frequent character.

Program to find the least frequent character

# Python program to find the least frequent character of the string
import collections 
# Getting string input from the user 
myStr =  input('Enter the string : ')

# Finding the least frequent character of the string 
freq = collections.Counter(myStr)
leastFreq = min(freq, key = freq.get)

# Printing values 
print("Entered String is ", myStr)
print(leastFreq , "is the least frequent character with frequency of " , freq[leastFreq])

Output:

RUN 1:
Enter the string : Python programming
Entered String is  Python programming
h is the least frequent character with frequency of  1

RUN 2:
Enter the string : AAABBBCDDD
Entered String is  AAABBBCDDD
C is the least frequent character with frequency of  1

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to split and join a string... >>
<< Python program to accept the strings which contain...