K’th Nonrepeating Character in Python (original) (raw)

K’th Non-repeating Character in Python

Last Updated : 14 Nov, 2025

Given a string 's' and an integer 'k', the task is to find the K’th non-repeating character in the string. A non-repeating character is one that appears exactly once.

**Example:

**Input: "geeksforgeeks"
k=3

**Output: "r"

**Explanation: In "geeksforgeeks", the characters that appear only once are f, o, and r. The third non-repeating character is "r".

Using collections.Counter

Counter quickly counts how many times each character appears, making it easy to pick out non-repeating ones.

Python `

from collections import Counter s = "geeksforgeeks" k = 3

c = Counter(s) res = [ch for ch in s if c[ch] == 1]

print(res[k - 1] if k <= len(res) else None)

`

**Explanation:

Using OrderedDict

OrderedDict keeps track of the order characters appear while counting them.

Python `

from collections import OrderedDict

s = "geeksforgeeks" k = 3

freq = OrderedDict() for ch in s: freq[ch] = freq.get(ch, 0) + 1

res = [ch for ch, cnt in freq.items() if cnt == 1] print(res[k - 1] if k <= len(res) else None)

`

**Explanation:

Using Regular Dictionary

This method uses a normal dictionary to count characters, then collects the non-repeating ones.

Python `

s = "geeksforgeeks" k = 3

freq = {} for ch in s: freq[ch] = freq.get(ch, 0) + 1

res = [] for ch in s: if freq[ch] == 1: res.append(ch)

print(res[k - 1] if k <= len(res) else None)

`

**Explanation:

Using List Comprehension

This approach counts frequencies and filters non-repeating characters using list comprehension.

Python `

s = "geeksforgeeks" k = 3

freq = {} for ch in s: freq[ch] = freq.get(ch, 0) + 1

res = [ch for ch in s if freq[ch] == 1] print(res[k - 1] if k <= len(res) else None)

`