2

I have the code, listed below, which I am trying to get to remove any duplicate football team names from a string vector. However, it is only working sometimes, it will remove duplicate names for some of the teams; but then for others there will be multiple occurrences of the same team name in the final array.

For example it would print:

aresnal
wigan
villa
liverpool
villa

Notice there are two 'villa' names, could anyone give me a suggestion? The 'finalLeague' is the array which is storing all of the names, and is the array which needs the duplicates removing out of.

  for (int i = 0;i < finalLeague.size();i++)
  {       
      string temp = finalLeague[i];
      int h = i + 1;
      for (int j = i+1;j < finalLeague.size();j++)
      {
          if (finalLeague[j] == finalLeague[i])
          {               
              finalLeague.erase(finalLeague.begin()+j);     
          }     
      }
  }
1
  • it will be easy to check when you add an element : if the element already exist don't add it . Commented Dec 17, 2013 at 13:57

3 Answers 3

7

Sure, you can use a combination of std::sort, std::unique and std::vector::erase:

std::sort(finalLeague.begin(), finalLeague.end());
auto it = std::unique(finalLeague.begin(), finalLeague.end());
finalLeague.erase(it, finalLeague.end());

Alternatively, use a container that does not accept duplicates in the first place:

std::set<std::string> finalLeague;           // BST, C++03 and C++11
std::unordered_set<std::string> finalLeague; // hash table, C++11
Sign up to request clarification or add additional context in comments.

Comments

0

This can also be done using a hashmap. Using #include <unordered_map> will let you use it. Note that you might have to use C++ 11 for it. Read about unordered maps here.

All you need to do is check whether the string has occurred before or not and keep pushing unique strings into a new vector.

USP of this method is that it needs minimal amount of code. Just one loop would do the trick.

4 Comments

It not available since c++11. It's officially standardized since c++11. It is available in many popular c++ compilers as an extension since long ago.
In gcc it was present already in 3.4 as hash_map. There is a bugfix report in change log. And this dates back to 2006. I believe that vc++ had something similar. But of course there might be differences, although my point is simply if one has and old pre-c++11 compiler, they should not get discouraged in case they need similar functionality.
std::map will also work. It's the map part that matters initially, not the implementation detail.
I guess my answer should have been just that he could also use hashmaps.
0

you should use std::unique

std::vector<std::string> vec;
// filling vector
// ....

std::vector<std::string>::iterator it;
it = std::unique (vec.begin(), vec.end());
vec.resize(std::distance(vec.begin(),it)); 

@edit: as @Gorpik said, vector must be sorted before use std::unique, otherwise only equal consecutive elements will be deleted.

1 Comment

std::unique only removes consecutive duplicates. You would need to sort the vector before using it.

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.