

- 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
Python program to Find the first non-repeating character from a stream of characters?
In this section we are going to find the first unique or non-repeating character from a string or stream of characters. There are multiple ways to solve this problem. We will try to create two different program for the same stream of characters.
Method 1: Using function
def firstNonRepeatingChar(str1): char_order = [] counts = {} for c in str1: if c in counts: counts[c] += 1 else: counts[c] = 1 char_order.append(c) for c in char_order: if counts[c] == 1: return c return None print(firstNonRepeatingChar('PythonforallPythonMustforall')) print(firstNonRepeatingChar('tutorialspointfordeveloper')) print(firstNonRepeatingChar('AABBCC'))
Result
M u None
Above program give O(n) solution. In above program we first loop through the string once. Once we find a new character, we store it in counts object with a value of 1 and append it to char_order. When we come across a repeated character, we increment the value of counts by 1. Finally, we loop through char_order until we find a character with a value of 1 in char_order and return it.
Method 2: Using while loop
s = "tutorialspointfordeveloper" while s != "": slen0 = len(s) ch = s[0] s = s.replace(ch, "") slen1 = len(s) if slen1 == slen0-1: print ("First non-repeating character is: ",ch) break; else: print ("No Unique Character Found!")
Result
First non-repeating character is: u
- Related Questions & Answers
- Find the first non-repeating character from a stream of characters in Python
- Java program to Find the first non-repeating character from a stream of characters
- Finding first non-repeating character JavaScript
- Finding the first non-repeating character of a string in JavaScript
- Find first repeating character using JavaScript
- How to find its first non-repeating character in a given string in android?
- First non-repeating character using one traversal of string in C++
- Find the last non repeating character in string in C++
- Write a program to find the first non-repeating number in an integer array using Java?
- Queries to find the last non-repeating character in the sub-string of a given string in C++
- Return index of first repeating character in a string - JavaScript
- First non-repeating in a linked list in C++
- Python Program to Remove the nth Index Character from a Non-Empty String
- Finding the index of the first repeating character in a string in JavaScript
- Detecting the first non-repeating string in Array in JavaScript
Advertisements