Connor Schwinghammer Connor Schwinghammer - 1 year ago 68
C++ Question

Removing duplicate elements of a vector of pairs based on individual elements inside the pair

So I need to find some most common used words in a file.

I have a

vector<pair<string, int> > wordList
which keeps track of every word in the file along with its frequency. This part works fine.

The problem is, the output shows multiple versions of each word. This is because the way I calculated it was:

  • load all words into the vector with a frequency of 1

  • Go through again and if the word shows up twice, increase its count

  • The part that I need help on, which is to delete multiple entries of the same word.

    for(int j = 0; j < wordList.size(); j++)

    for(int k = 0; k < wordList.size(); k++)
    if(wordList[j].first == wordList[k].first && k != j)

    sort(wordList.begin(), wordList.end(), sort_compare_string);
    wordList.erase(unique(wordList.begin(), wordList.end()));

This is my current approach. This function tallys up all the word. the problem is that the line
that's inside the for loop
produces an out of bounds error, so I cant remove the duplicate entry that way.
I also tried the unique() approach, but that doesn't seem to work it only removes some entries.

What is the most efficient way to reduce a vector of pairs to only unique elements?

Answer Source

You may use something like:

std::map<std::string, std::size_t>
compute_frequency(const std::vector<std::string>& words)
    std::map<std::string, std::size_t> res;

    for (const auto& word : words) {
    return res;

void test(const std::vector<std::string>& words)
    const auto m = compute_frequency();
    std::vector<std::pair<std::string, std::size_t>> v(m.begin(), m.end());

    auto myless = [](const auto& lhs, const auto& rhs) {
        //return lhs.first > rhs.first;   // by decreasing word
                                          // (then you may add the comp in map directly)
        return lhs.second > rhs.second;   // by decreasing frequency
    std::sort(v.begin(), v.end(), myless);
    for (const auto& p : v) {
        std::cout << p.first << " appears " << p.second << std::endl;


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download