how to find duplicates in std::vector<string> and return a list of them?
c++, functor, stl
Solution
- Make an empty `std::unordered_set<std::string>`
- Iterator your vector, checking whether each item is a member of the set
- If it's already in the set, this is a duplicate, so add to your result list
- Otherwise, add to the set.
Since you want each duplicate only listed once in the results, you can use a hashset (not list) for the results as well.
Problem
So if I have a vector of words like: ``` Vec1 = "words", "words", "are", "fun", "fun" ``` resulting list: "fun", "words" I am trying to determine which words are duplicated, and return an alphabetized vector of 1 copy of them. My problem is that I don't even know where to start, the only thing close to it I found was `std::unique_copy` which doesn't exactly do what I need. And specifically, I am inputting a `std::vector<std::string>` but outputting a `std::list<std::string>`. And if needed, I can use functor. Could someone at least push me in the right direction please? I already tried reading stl documentation,but I am just "brain" blocked right now.