removing duplictes

I have a homework for learning functions.
I have to get a string from user and first I must arrange it in alphabetic order and the I must remove the duplicates.
for finding it I used 2 loops that first one gets a letter and the second one will check for it in the rest of string
my problem is that I don't know that when I find the duplicate how should I remove it
or at least put it in another string without the duplicates
PS. I'm not allowed to touch the int main()

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
29
30
31
32
33
34
35
36
37
38
39
40
  #include <iostream>
using namespace std;
string sortMyString(string myString);
string removeRepetitive(string myString);
int main()
{
        string str;
        cin>>str;
    cout<<sortMyString(removeRepetitive(str));
    return 0;
}
string sortMyString(string myString){
	string alph="abcdefghijklmnopqrstuvwxyz";
	string s=" ";
	int i,j,k=0;
	for(i=0;i<alph.length();i++){
		for(j=0;j<myString.length();i++){
			if(myString[j]==alph[i]){
			s=myString[j]+s;	
			}
		}
	}
	return s;
}
// s is the sorted string
string removeRepetitive(string myString){
	string x= sortMyString(myString);
	string y=" ";
	int i,j;
	for(i=0;myString.length();i++){
		for(j=i+1;j<myString.length();j++){
			if(x[i]==x[j]){
			x=x-x[j];
			}
//the problem is with this part.
		}
	}
	return x;
}
Last edited on
The string has an erase() function. See:

http://www.cplusplus.com/reference/string/string/erase/
For removing duplicates from a string that is already sorted, loop through the characters of this string and add the character to a result string if it differs from its predecessor.

You could also let the STL do all the hard work for you:
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
29
30
31
#include <iostream>
#include <string>
#include <set>
using namespace std;

string sortAndRemoveDuplicates( string str );

//==========================================================

int main()
{
   string str;
   cout << "Enter a string: ";
   getline( cin, str );

   cout << "Final string is: " << sortAndRemoveDuplicates( str ) << '\n';
}

//==========================================================

string sortAndRemoveDuplicates( string str )
{
   set<char> temp;
   string result;

   for ( char c : str  ) temp.insert( c );
   for ( char c : temp ) result += c;
   return result;
}

//========================================================== 


Enter a string: remove duplicates from here
Final string is:  acdefhilmoprstuv


Note that you may wish to consider what to do about upper and lower case: decide first of all whether they should be the same letter or not.
is there any way to do so with out using anything else other than #include <iostream>?
I' not allowed to use other #include<>
Well, you probably ought still to have #include <string>

Quoting my previous post:
For removing duplicates from a string that is already sorted, loop through the characters of this string and add the character to a result string if it differs from its predecessor.


Once you have your own sorting routine, then you can replace sort() in the code below and then remove the #include <algorithm> .

You still need to consider what to do about upper and lower case. If they are distinct then fine as it is. If they are to be taken as the same, then this will affect all comparisons in the sorting and the equality test before adding to result.

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
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <algorithm>         // for sort(); you can replace with your own and remove this include
using namespace std;

string sortAndRemoveDuplicates( string str );

//==========================================================

int main()
{
   string str;
   cout << "Enter a string: ";
   getline( cin, str );

   cout << "Final string is: " << sortAndRemoveDuplicates( str ) << '\n';
}

//==========================================================

string sortAndRemoveDuplicates( string str )
{
   if ( str.size() <= 1 ) return str;                // trivial and unlikely case

   sort( str.begin(), str.end() );                   // replace with your own sorting routine

   string result;   result += str[0];
   for ( int i = 1; i < str.size(); i++ )
   {
      if ( str[i] != str[i-1] ) result += str[i];    // As long as str is sorted, not equal to predecessor ...
   }                                                 // ... means it is not a duplicate; so add to result
   return result;
}

//==========================================================  


Enter a string: remove duplicates from here
Final string is: acdefhilmoprstuv




If you want to remove duplicates without sorting first then here are a couple of options. (The latter has some dubious type conversions, and it's difficult to make it more portable without some extra #include<> statements.)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
using namespace std;

string RemoveDuplicates1( string str );
string RemoveDuplicates2( string str );

//==========================================================

int main()
{
   string str;
   cout << "Enter a string: ";
   getline( cin, str );

   cout << "Final string is: " << RemoveDuplicates1( str ) << '\n';
   cout << "Final string is: " << RemoveDuplicates2( str ) << '\n';
}

//==========================================================

string RemoveDuplicates1( string str )               // O(N^2) algorithm - actually not!
{
   if ( str.size() <= 1 ) return str;                

   string result;
   for ( int i = 0; i < str.size(); i++ )
   {
      bool seen = false;
      for ( int j = 0; j < i; j++ )                  // test all previous
      {
         if ( str[i] == str[j] )
         {
            seen = true;
            break;
         }
      }
      if ( !seen ) result += str[i];                 // if not used previously then add to result
   }                 
   return result;
}

//========================================================== 

string RemoveDuplicates2( string str )               // O(N) algorithm
{
   if ( str.size() <= 1 ) return str;                

   const int MAXCHARS = 256;                         // WARNING: needs to be the size of your character set
   bool seen[MAXCHARS] = { 0 };                      // indicates occurrence of this character number

   string result;
   for ( int i = 0; i < str.size(); i++ )
   {
      char c = str[i];
      if ( !seen[c] ) result += c;
      seen[c] = true;
   }                 
   return result;
}

//==========================================================  


Enter a string: remove duplicates from here
Final string is: remov duplicatsfh
Final string is: remov duplicatsfh
Last edited on
Topic archived. No new replies allowed.