DELETE DUPLICATEs in C++

Anyone know how to make this program??? The program goes like this.
OUTPUT:
string: aabnghhbf
result: nfg
Yes

1
2
3
4
5
#include <iostream>
int main() {
  std::cout << "string: aabnghhbf\nresult: nfg\n";
  return 0;
}
Last edited on
std::cout << "string: aabnghhbf\nresult: nfg\n";

This code were only going to print my sample output.
closed account (zb0S216C)
Look into string::erase( )[1].

References:
[1]http://www.cplusplus.com/reference/string/string/erase/

Wazzak
Last edited on
You can use an int array to hold the times each letter appears in your string -> int counter[26] = { 0 };

First, scan your string and update your counter array appropriately.

Then, scan your counter array and only print when counter[i] == 1.

Example for string == "aabcddde":

First...

(a)abcddde -> counter[0]++;

a(a)bcddde -> counter[0]++;

aa(b)cddde -> counter[1]++;

aab(c)ddde -> counter[2]++;

aabc(d)dde -> counter[3]++;

aabcd(d)de -> counter[3]++;

aabcdd(d)e -> counter[3]++;

aabcddd(e) -> counter[4]++;

Then...

counter[0] == 2 -> don't print 'a'

counter[1] == 1 -> print 'b'

counter[2] == 1 -> print 'c'

counter[3] == 3 -> don't print 'd'

counter[4] == 1 -> print 'e'
Last edited on
ok tnx.
This is my last question


I am trying to run my program but nothings appear.. what is the problem with this??



#include<iostream>
#include<conio.h>
using namespace std;
//counting characters
int main(){
char counter[20]={0};

int i;
int j=0;

string str="aaabdkkl";

while(str[j]!='\0')//to count string in count1
{
j++;
}
for(i=j;i<counter[j];i++){

if(counter[i]==1){


cout<<str;
}
}

getch();
return 0;
}
What do you mean by "nothings appear"?
"nothings appear"


When I am trying to run my program it only shows the black screen and there is no output shows there.
Try this:

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<conio.h>
using namespace std;
//counting characters
int main(){
char counter[20]={0};

int i;
int j=0;

string str="aaabdkkl";

while(str[j]!='\0')//to count string in count1
{
j++;
}
for(i=j;i<counter[j];i++){

if(counter[i]==1){


cout<<str;
}
}


cout << flush(); // or cout << endl(); // flushes the out stream (i.e. displays it)

getch();
return 0;
}


edit:
The main reason are the calculations in previous lines.
So my fix shouldn't help.
Last edited on
I feel generous today. I'll give you the code you need, but
you'll have to put the lines in the correct order yourself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
cout << char(i + 'a');
string str = "aaabdkkl";
using namespace std;
int main()
cout << "\n(hit enter to quit...)";
int i;
#include <string>
return 0;
for (i = 0; i < str.size(); ++i)
for (i = 0; i < 26; ++i)
#include <iostream>
cin.get();
if (counter[i] == 1)
int counter[26] = { 0 };
++counter[str[i] - 'a'];
}


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
 
int main(){
char counter[20]={0};

int i;
int j=0;

string str="aaabdkkl";

while(str[j]!='\0') // So we count number characters until we hit a null terminator. 
// Problem is, strings are not null terminated AND depending on the implementation 
// this would yield a "out of range" exception or just end in a endless loop.
{
j++;
}
for(i=j;i<counter[j];i++){ // We set i = j but j is the string length. How could "i" possible be less than 
// "counter[ i ]" which is 0

if(counter[i]==1){


cout<<str; // Isn't "str" the whole string, you didn't change it in anyway
}
}


Seems like you messed up C & C++ better read the documentation how to use strings more carefully ;)
Last edited on
ok.. tnx for your help. My problem was solve.
Topic archived. No new replies allowed.