C++ Program To Find Duplicate characters in a string

Find Duplicate characters in a string

Today in this article we will discuss how to Find Duplicate characters in a string and we will be using C++ programming language. We will also disscuss various methods to do the porblem .

Lets understand this with the help of example :- 

  • Input:- Prepinsta Prime
  • Output:- Duplicate characters in a given string:
    P
    r
    e
    i

Here the occurence of P,r,e,i is more than once,so that is the output

Find Duplicate characters in a string

Algorithm

  • Define a string and take the string as  input form the user. 
  • Two loops will be used to find the duplicate characters. Outer loop will be used to select a character and then initialize variable count by 1 its inside the outer loop so that the count is updated to 1 for every new character.
  • Inner loop will compare the selected character with the rest of the characters present in the string.
  • If a match is found, it increases the count by 1 and set the duplicates of selected characters by ‘0’ to mark them as visited.
  • After the inner loop, if the count of characters is greater than 1, then it has duplicates in the string.
Find-Duplicates-Characters-in-Cpp

C++ Code :-

#include <iostream> 
#include <string.h>
using namespace std;

int main(){
string str;
int count;
cout<<"Enter the string : \n";
getline(cin,str);

cout<<"Duplicate characters in a given string: \n";
//Counts each character present in the string

int l=str.length();
for(int i = 0; i <l; i++) {
count = 1; //Updates the count again to 1 for every new character

for(int j = i+1; j < l; j++) {
if(str[i] == str[j] && str[i] != ' ') {
count++;
//Set string[j] to 0 to avoid printing the visited character
str[j] = '0';
}
}

//A character is considered as duplicate if count is greater than 1

if(count > 1 && str[i] != '0')
cout<<str[i];
}

return 0;
}

Output:-

Enter the string : 
prakriti vedi
Duplicate characters in a given string: 
r
i
Enter the string : 
prepinsta
Duplicate characters in a given string:
p
Enter the string :
Aishwariya prasad 
Duplicate characters in a given string: 
i
s
a
r

Method 2

  • Here we are storing the frequency of characters in a map called count
  • Iterating over the count map and if the frequency of any of the character is more than one then we will print the character.
  // C++ program to count all duplicates from string using maps
  #include <bits/stdc++.h>
  using namespace std;
  void printDuplicates(string str)
  {
   map<char, int> count;
   for (int i = 0; i < str.length(); i++) {
    count[str[i]]++;
}
  cout << "Duplicate characters in a given string:"; 
for (auto it : count) {
if (it.second > 1) cout<< it.first<< " "; } } // Driver code int main() { string str ; cout<<"Enter the string : \n"; getline(cin,str); printDuplicates(str); return 0; }
Enter the string : 
prep instaa
Duplicate characters in a given string:a p
Enter the string : 
prepinsta
Duplicate characters in a given string:
p