mercredi 21 décembre 2022

How do i remove every odd time appeared character in string? [closed]

I have a string that contains only A, B, C, D. I need to remove all odd-appeared characters in this string. For example if string is "ABBBCA" it will turn into "BA" (first "A" removed because it was first time "A" appeared, then first and third "B" removed, and "C" also, because it was first time it appeared). I'm beginner in coding, so i'm sorry if this question is too simple or something.

I have tried something like this:

#include <iostream>
#include <string>

using namespace std;
int main()
{
    string firstchars = "ABBBCA";
    for (int o = 0; o < firstchars.length(); o ++) {
        firstchars.erase(o,1);
    }

But it just erases every odd element in string, not every odd "A", "B", "C" and "D". And i have "BBA" as output.

Aucun commentaire:

Enregistrer un commentaire