2

Well, of course, this can be done easily with 2 vectors.

Code:

vector<int> A;
vector<int> B;
bool ok = true;
for(auto x: A){
    for(auto y: B) {
        if(x==y) {
            ok = false;
            break;
        }
    }
    if(ok) {
        B.push_back(x);
    }
    ok = true;
}

But, can this be without having 15 lines of code ? What's the simplest way you would do this ?

Keep in mind that we need to have the same order elements appeared in vector.

  • Consecutive duplicates, or any duplicates? – tadman May 02 '19 at 21:12
  • 1
    If the duplicates can be anywhere, I'd suggest [`std::copy_if`](https://en.cppreference.com/w/cpp/algorithm/copy), with a suitable [lambda](https://en.cppreference.com/w/cpp/language/lambda) using [`std::find`](https://en.cppreference.com/w/cpp/algorithm/find) of the destination vector. – Some programmer dude May 02 '19 at 21:18
  • 2
    What do you mean by the best way? The fastest? The least memory consuming one? or the shortest? – TonySalimi May 02 '19 at 21:25
  • 1
    How do you keep the order of the elements when removing the duplicates from, for example, `{1, 2, 1}`? Without duplicates it will not be possible to preserve both the order `1` followed by `2` and the order `2` followed by `1`. (Unless you only want to preserve the order of the first occurrence of a value?) – JaMiT May 02 '19 at 21:29

0 Answers0