

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
K’th Non-repeating Character in Python using List Comprehension and OrderedDict
In this article, we will learn about K’th Non-repeating Character in Python using List Comprehension and OrderedDict. To do so we take the help of inbuilt constructs available in Python.
Algorithm
1. First, we form a dictionary data from the input. 2. Now we count the frequency of each character. 3. Now we extract the list of all keys whose value equals 1. 4. Finally, we return k-1 character.
Example
from collections import OrderedDict import itertools def kthRepeating(inp,k): # returns a dictionary data dict=OrderedDict.fromkeys(inp,0) # frequency of each character for ch in inp: dict[ch]+=1 # now extract list of all keys whose value is 1 nonRepeatDict = [key for (key,value) in dict.items() if value==1] # returns (k-1)th character if len(nonRepeatDict) < k: return 'no ouput.' else: return nonRepeatDict[k-1] # Driver function if __name__ == "__main__": inp = "tutorialspoint" k = 3 print (kthRepeating(inp, k))
Output
a
Conclusion
In this article, we found the K’th Non-repeating Character in Python using List Comprehension and OrderedDict.
- Related Questions & Answers
- Finding first non-repeating character JavaScript
- Python List Comprehension and Slicing?
- Python List Comprehension?
- Check order of character in string using OrderedDict( ) in Python
- First non-repeating character using one traversal of string in C++
- Count set bits using Python List comprehension
- Nested list comprehension in python
- Find the last non repeating character in string in C++
- OrderedDict in Python
- Find the first non-repeating character from a stream of characters in Python
- First non-repeating in a linked list in C++
- Find first repeating character using JavaScript
- Find k-th character of decrypted string - Set – 2 in Python
- Finding the first non-repeating character of a string in JavaScript
- Python program to Find the first non-repeating character from a stream of characters?
Advertisements