Python program to find non repeating characters in a string
Find Non Repeating Characters
The string is a combination of characters when 2 or more characters join together it forms string whether the formation gives a meaningful or meaningless output. In python programming, we treat a single character also as a string because there is no datatype as a character in python. In this python program, we will find unique elements or non repeating elements of the string.
Algorithm
- Step 1:- store the string in a varaible lets say String.
- Step 2:- lets it be “prepinsta”.
- Step 3:- Start iterating through string.
- Step 4:- Initialize count variable.
- Step 5:- Again start iterating through same string.
- Step 6:- Increment count variable as character is found in string.
- Step 7:- If count is more then 2 break the loop.
- Step 8:- If count is 1 print the character.
- Step 9:- End.
Python Code:- Find Non Repeating Characters
#take user input String = "prepinsta" for i in String: #initialize a count variable count = 0 for j in String: #check for repeated characters if i == j: count+=1 #if character is found more than 1 time #brerak the loop if count > 1: break #print for nonrepeating characters if count == 1: print(i,end = " ")
Output
r e i n s t a
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment
s=input()
d=””
for i in s:
fre=s.count(i)
if fre>1:
pass
else:
d+=i
print(d)
s = input();
d = {};
for i in s :
if i in d:
d[i] += 1;
else :
d[i] = 1;
for i in d.values() :
if i == 1:
print(d.keys());
break;
a=input()
for i in a:
if(a.count==1):
print(i,end=””)
s=”hello world”
s1=””
count=0
for i in s:
count=s.count(i)
if count>1:
pass
else:
s1=s1+i
print(s1)
str = input(‘Enter the string :’)
x=list(dict.fromkeys(str))
for i in x:
if str.count(i)==1:
print(i,end=””)
s=str(input(“Enter the string:”))
string=str()
for i in s:
if s.count(i)>1:
pass
else:
string=string+i
print(string)
from collections import Counter
s = input(‘Enter the string :’)
s = Counter(s)
for k in s:
if s.get(k) == 1:
print(k,end=” “)
n = input(“enter the string:”)
for i in n:
if n.count(i) == 1:
print(i, end=”)
s=input()
for i in s:
count=0
if(s.count(i)>1):
count=1
else:
count=0
if(count==0):
print(i,end=”)
// Here is my java program
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Program14 {
static void foundUnique(String s1) {
Map map = new HashMap();
for (int i = 0; i < s1.length(); i++) {
if (map.containsKey(s1.charAt(i)))
map.put(s1.charAt(i), map.get(s1.charAt(i)) + 1);
else
map.put(s1.charAt(i), 1);
}
Set keys = map.keySet();
for (Character ch : keys) {
if (map.get(ch) == 1)
System.out.print(ch + ” “);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.print(“Enter the String : “);
String s1 = sc.nextLine();
foundUnique(s1);
}
}
String = input(‘Enter the String :’)
for i in String:
if String.count(i)<2:
print(i, end=" ")
Another better approach:-
st=”ChampakChacha”
for i in st:
if i!=” “:
if (st.count(i)==1):
print(i, end=” “)