0

I have a vector of

strings.vector input;

bob 
sam
bob 
sammom
aardvark    
money
aardvark    
wanted

I need to remove the duplicates but each part of the vector corresponds to the next location. They are pairs.

ex. bob corresponds to the its definition, which is sam.

I need to keep the first instance, so keep bob and sam, but remove the second instance of bob, so remove bob and sammon. Only the first instance of the pair need to kept.

It doesn't matter if the sam and sammon don't match, all that matters is the first part of the pair.

The vector is already in alphabetical order. I can't use the algorithm library.

3
  • 4
    We need more information here: What language is this? What have you attempted? Commented Mar 18, 2014 at 21:49
  • Its in C++, I haven't attempted anything because I have no idea how to go about this. Commented Mar 18, 2014 at 21:51
  • Just use the algorithm library. That's what it's for. Commented Mar 19, 2014 at 1:01

2 Answers 2

1

Something like this?

#include <iostream>

using namespace std;

int main()
{
    int stringSize=10, k=2, newlen=0;

    string v[] = {"bob","sam","bob","samelia","bob","sammom","aardvark","money","aardvark","wanted"};

    // erase duplicates
    for (int i=0; i<=stringSize-4; i+=2)
    if (v[i]==v[i+k])
    {
        while (v[i]==v[i+k])
        {
            v[i+k]="";
            v[i+k+1]="";

            k+=2;
        }
        i=k-2;
        k=2;
    }

    // remove blank components
    for (k=0; k<stringSize; k++)
    if (v[k]!="")
    {
        v[newlen]=v[k];
        newlen++;
    }
    stringSize=newlen;

    // show the cleaned vector
    for (int i=0; i<stringSize; i++)
    cout << v[i] << endl;

}
Sign up to request clarification or add additional context in comments.

6 Comments

Yes, I used a vector of strings with your example values.
I just improved the code. Now will work for infinite consecutive pairs :)
Actually i just stop at the last filled post, which is given by the stringsize. thanks
What would I do about because the duplicates if its not necessary the second thats a duplicate, what if its the third?
Works for infinite consecutive pairs. You want it to remove just the second duplicate?
|
0

This will remove duplicates even if there is multiple in a row like this

Again note that this is for pairs

And only the first instance is kept. Lets say vector input; has the following

bob
son
bob
son2
mark
wife1
mark
wife2
mark
wife3

Becomes

bob
son
mark
wife

This will remove duplicates up to 6 in a row, increase as needed.

I had to remove the blank spaces two times for some reason.

    //vector<string> input; is filled in alphabetical order with duplicates
    int size=input.size();

    //transfer to dynamically created array of strings
    string *strarray;
    strarray = new string[size];
    for(int i=0; i<size; i++){
       strarray[i] = input[i];//Copy the vector to the string
    }

    // erase duplicates
    for (int i=0; i<=size-(size/2); i+=2){
        if (strarray[i]==strarray[i+2])
        {
            strarray[i+2].erase(0,strarray[i+2].size());
            strarray[i+3].erase(0,strarray[i+3].size());
        }
        if (strarray[i]==strarray[i+4])
        {
            strarray[i+4].erase(0,strarray[i+4].size());
            strarray[i+5].erase(0,strarray[i+5].size());
        }
        if (strarray[i]==strarray[i+6])
        {
            strarray[i+6].erase(0,strarray[i+6].size());
            strarray[i+7].erase(0,strarray[i+7].size());
        }
        if (strarray[i]==strarray[i+8])
        {
            strarray[i+8].erase(0,strarray[i+8].size());
            strarray[i+9].erase(0,strarray[i+9].size());
        }
        if (strarray[i]==strarray[i+10])
        {
            strarray[i+10].erase(0,strarray[i+10].size());
            strarray[i+11].erase(0,strarray[i+11].size());
        }
    }

    // remove blank components
        for (int i=0; i<size; i++){
        if (strarray[i]=="")        {
            for (int j=i; j<size-1; j++)
                strarray[j]=strarray[j+1];

            size--;
            i--;
        }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.