C++ program to find non repeating characters in a string
Finding non repeating characters in a string.
In this article we will learn how to code a C++ program to find non repeating characters in a string. First we will calculate the frequency of each character present in the string as non repeating characters are those that are present in the string only once. To calculate the frequency we will use a for loop that will count how many times every unique character is present in the string. And then we will use another for loop to print those characters that have frequency one as these characters are non repeating.
Algorithm:
- Initialize the variables and accept the input.
- Initialize a for loop.
- This for loop will calculate the frequency of each character.
- Terminate this for loop at the end of string.
- Print the characters having frequency one using another for loop.
C++ Code for finding non repeating characters in a string
#include <iostream> using namespace std; int main() { //Initializing variables. char str[100]="prepinsta"; int i; int freq[256] = {0}; //Calculating frequency of each character. for(i = 0; str[i] != '\0'; i++) { freq[str[i]]++; } cout<<"The non repeating characters are: "; for(i = 0; i < 256; i++) { if(freq[i] == 1)//Finding non repeating charcters and printing them. { cout<<char(i)<<" " ; } } return 0; }
Output
Enter the string: prepinsta The non repeating characters are: a e i n r s t
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
//c++ code//
#include
using namespace std;
int main(){
string str;
getline(cin,str);
map mp;
for(int i=0;str[i]!=’\0′;i++){
mp[str[i]]++;
}
for(auto itr :mp){
if(itr.second==1){
cout<<itr.first<<" ";
}
}
return 0;
}
// Here is my java program to find non repeating characters in a string
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);
}
}
java program :
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(“Enter String : “);
String str = sc.nextLine();
int[] freq = new int[str.length()];
int i, j;
//Converts given string into character array
char string[] = str.toCharArray();
for(i = 0; i <str.length(); i++) {
freq[i] = 1;
for(j = 0; j <str.length(); j++) {
if(string[i] == string[j] && i != j) {
freq[i]++;
}
}
}
// Non corresponding frequencies
System.out.println("Non corresponding frequencies:");
for(i = 0; i <freq.length; i++) {
if(freq[i] == 1)
System.out.print(string[i] + " ");
}
}
}
#include
#include
using namespace std;
int main()
{
string str;
getline(cin,str);
int len=str.size();
sort(str.begin(),str.end());
for(int i=0;i<=len;i++)
{
if(str[i]==str[i+1])
{
i++;
continue;
}
else{
cout<<str[i]<<" ";
}
}
return 0;
}