Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,924 questions

51,857 answers

573 users

How to remove all duplicate characters from a string in C++

3 Answers

0 votes
#include <bits/stdc++.h> 

using namespace std; 
  
void remove_duplicate_characters(char s[], int len) { 
    set<char>st (s, s + len - 1); 
  
    int i = 0; 
    for (auto ch : st) 
        s[i++] = ch; 
    s[i] = '\0'; 
} 
  
int main() 
{ 
   char s[] = "c++ prograaaammmming";
   int len = sizeof(s)/sizeof(s[0]);
   
   remove_duplicate_characters(s, len); 
   
   cout << s; 
   
   return 0; 
} 



/*
run:

 +acgimnopr
 
*/

 



answered Oct 27, 2019 by avibootz
0 votes
#include <bits/stdc++.h> 
 
using namespace std; 
   
int main() 
{ 
    string s = "c++ prograaaammmming";
     
    sort(s.begin(), s.end()); 
   
    auto no_dup = unique(s.begin(), s.end()); 
 
    s = string(s.begin(), no_dup);
    
    cout << s;
 
    return 0; 
} 
 
 
 
/*
run:
 
 +acgimnopr
  
*/

 



answered Oct 27, 2019 by avibootz
edited Oct 27, 2019 by avibootz
0 votes
#include <bits/stdc++.h> 

using namespace std; 

string remove_duplicate_characters(string s) {
    if (s.begin() == s.end()) return s;
    
    auto no_dup = s.begin(); 
    
    for (auto current = no_dup; current != s.end();) {
        current = find_if(next(current), s.end(), [no_dup](const char ch) {
                          return ch != *no_dup; 
                  });
        *++no_dup = move(*current);;
    }
    s.erase(no_dup, s.end());
    
    return s;
}
  
int main() 
{ 
    string s = "c++ prograaaammmming";
    
    s = remove_duplicate_characters(s);

    cout << s;

    return 0; 
} 



/*
run:

c+ programing
 
*/

 



answered Oct 27, 2019 by avibootz

Related questions

2 answers 146 views
1 answer 152 views
2 answers 178 views
3 answers 148 views
2 answers 124 views
2 answers 186 views
...