Python Least Frequent Character in String (original) (raw)

Last Updated : 4 Nov, 2025

Given a string, the task is to identify the character that appears the least number of times. **For example, in "GeeksforGeeks", the character 'f' occurs only once, so it is the least frequent.

Let’s explore multiple methods to find the least frequent character in Python.

Using collections.Counter

The most efficient way to do this is by using collections.Counter which counts character frequencies in one go and makes it easy to find the least frequent character.

Python `

from collections import Counter s = "GeeksforGeeks" freq = Counter(s) res = min(freq, key=freq.get) print(str(res))

`

**Explanation:

Using a Dictionary

Using a dictionary, we count the frequency of each character in a single pass and directly find the least frequent character. This approach avoids re-scanning the string making it more efficient than naive methods.

Python `

s = "GeeksforGeeks" d = {}
for char in s: d[char] = d.get(char, 0) + 1

res = min(d, key=d.get) print(str(res))

`

**Explanation:

Using Sorting

Using sorting, we first count the frequency of each character in the string and then sort the characters by their frequency in ascending order. The least frequent character is extracted from the first element of the sorted list.

Python `

s = "GeeksforGeeks" d = {}

for char in s: d[char] = d.get(char, 0) + 1

sorted_freq = sorted(d.items(), key=lambda item: item[1]) res = sorted_freq[0][0] print(res)

`

**Explanation:

Using str.count()

count() function counts the occurrences of each character and min() is used to identify the character with the lowest frequency. This approach is straightforward but becomes inefficient for larger strings due to its quadratic time complexity.

Python `

s = "GeeksforGeeks" res = min(s, key=lambda char: s.count(char)) print(str(res))

`

**Explanation: