snake plissken
snake plissken

Reputation: 2669

Erase duplicate element from a vector

I create a vector inside with several elements in c++ and I want to remove the elements of vector with the same values. Basically, I want to remove the whole index of the vector that is found a duplicate element. My vector is called person. I am trying to do something like:

for(int i=0; i < person.size(); i++){
    if(i>0 && person.at(i) == person.at(0:i-1)) { // matlab operator
        continue;
    }
    writeToFile( perason.at(i) );   
}

How is it possible to create the operator 0:i-1 to check all possible combinations of indexes?

Edit: I am trying GarMan solution but I got issues in for each:

        set<string> myset;
        vector<string> outputvector;

        for (string element:person)
        {
            if (myset.find(element) != myset.end())
            {
                myset.insert(element);
                outputvector.emplace_back(element);
            }
        }

Upvotes: 0

Views: 366

Answers (3)

fredoverflow
fredoverflow

Reputation: 263350

Here is an "in-place" version (no second vector required) that should work with older compilers:

std::set<std::string> seen_so_far;
for (std::vector<std::string>::iterator it = person.begin(); it != person.end();)
{
    bool was_inserted = seen_so_far.insert(*it).second;
    if (was_inserted)
    {
        ++it;
    }
    else
    {
        swap(*it, person.back());
        person.pop_back();
    }
}

Let me know if this works for you. Note that the order of elements is not guaranteed to stay the same.

Upvotes: 3

herohuyongtao
herohuyongtao

Reputation: 50717

If you can sort your vector, you can simply call std::unique.

#include <algorithm>    

std::sort(person.begin(), person.end());
person.erase(std::unique(person.begin(), person.end()), person.end());

If you cannot sort, you can use a hash-table instead by scanning the vector and update the hash-table accordingly. On the same time, you can easily check if one element is already existent or not in O(1) (and O(n) in total). You don't need to check all other elements for each one, which will be time-costly O(n^2).

Upvotes: 2

GarMan
GarMan

Reputation: 1044

Something like this will work

unordered_set<same_type_as_vector> myset;
vector<same_type_as_vector> outputvector;
for (auto&& element: myvector)
{
    if (myset.find(element) != myset.end())
    {
        myset.insert(element);
        outputvector.emplace_back(element); 
    }
}
myvector.swap(outputvector);

Code written into reply box, so might need tweaking.

Upvotes: 2

Related Questions