Removing duplicate characters from string

I am working on some game in c++ and i have to remove duplicate characters from one string i read something on net but i dont wanna just to copy/pase code from net because i dont understood it. Can anyone explain me some way for removing duplicate characters from string ?

Example: string str = "hello"

and than that string should become "helo"

Build a new string that's initially empty, while looping over the original string.

pseudocode:
1
2
3
4
5
string original = "hello"
string out
for each character in original string:
    if (out string does not contain character)
        append character to out string


There are more efficient ways to do this using maps or sorting and whatnot, but that's the basic logic.
the most basic way to do this is to make a new, empty string and append the next letter if it is not the same as the previous letter. Previous letter can default to '\0' safely for ascii for the first iteration.

you can do it in place or other fancy ways using the string library tools.
looks like, logically...

current letter = h
is h == to \0 ? no.
new string adds h.
current letter becomes e. previous is h.
is e== h? no, insert..
he
current letter becomes l
is l == e
no
insert: hel
current is second l
is l == l
yes, do nothing
hel
current becomes 0
is 0 == l
.. helo

Last edited on
Aleksa, should "chancellor" become "chanelor" or "chancelor"? (i.e. do you mean any at all duplicates in the word, reading left to right, or just adjacent duplicates?)

Gotta be specific for the computer, it can only handle things very literally.
Last edited on

Could it be like "zebraz" and change to "zebra"?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
#include <cctype>
using namespace std;


string removeDuplicates( const string &str )
{
   bool used[256]{};
   string result;
   for ( char c : str )
   {
      int n = tolower( c );
      if ( !used[n] ) 
      {
         result += c;
         used[n] = true;
      }
   }
   return result;
}


int main()
{
   string tests[] = { "Hello", "Bumblebee", "c++", "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch" };
   for ( string s : tests ) cout << s << ": " << removeDuplicates( s ) << '\n';
}
Last edited on
Example

Input

Input string: Programming in C.
Output

String after removing duplicate characters: Progamin C.


#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size

/* Function declarations */
void removeDuplicates(char * str);
void removeAll(char * str, const char toRemove, int index);


int main()
{
char str[MAX_SIZE];

/* Input string from user */
printf("Enter any string: ");
gets(str);

printf("String before removing duplicates: %s\n", str);

removeDuplicates(str);

printf("String after removing duplicates: %s\n", str);

return 0;
}


/**
* Remove all duplicate characters from the given string
*/
void removeDuplicates(char * str)
{
int i = 0;

while(str[i] != '\0')
{
/* Remove all duplicate of character string[i] */
removeAll(str, str[i], i + 1);
i++;
}
}

/**
* Remove all occurrences of a given character from string.
*/
void removeAll(char * str, const char toRemove, int index)
{
int i;

while(str[index] != '\0')
{
/* If duplicate character is found */
if(str[index] == toRemove)
{
/* Shift all characters from current position to one place left */
i = index;
while(str[i] != '\0')
{
str[i] = str[i + 1];
i++;
}
}
else
{
index++;
}
}
}
Output
Enter any string: Programming in C.
String before removing duplicates: Programming in C.
String after removing duplicates: Progamin C.

To know more about this visit at: http://www.cetpainfotech.com/technology/C-Language-Training
wow, to plug your training with spam, you turned <10 lines of code into a full page and used an N*N algorithm for a N problem. Makes me want to go there!

> gets(str);
Seriously, this is in your training material!?
Sheesh.

Topic archived. No new replies allowed.