Python program to find the least frequent character in the string

Here, we will take input from the user and find the least frequent character in the string in Python.
By Shivang Yadav Last updated : March 01, 2024

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Strings in Python are immutable means they cannot be changed once defined.

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.

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

To understand the above programs, you should have the basic knowledge of the following Python topics:

Python String Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.