C++

How to Find and Print Duplicate Words in std::vector using C++ STL Functions

In C++, finding and printing duplicate words in a std::vector<string> can be quite challenging. In this guide, we will walk you through the process of using some key STL functions to easily identify and output any duplicated words in your vector.

Find and Print Duplicate Words in std::vector<string> using STL functions

Finding and printing duplicate words in std::vector<string> can be done:

1: Using sort() and unique()

One simple way to find duplicate words is to sort the vector first and then use the std::unique() function to find unique elements. The unique() function returns an iterator to the end of the unique elements in the vector. We can then iterate over the vector and print the duplicate words.

For example:

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

int main() {

   vector<string> words = {"Black", "Brown", "Yellow", "Black", "Green", "Blue"};

   // Sort the vector to group duplicates together

   sort(words.begin(), words.end());

   // Find the duplicate words

   for (auto i = words.begin(); i != words.end(); i++) {

      auto count = count_if(i, words.end(), [=](const string& s) { return s == *i; });

      if (count > 1) {

cout << *i << " ";

      }

   }

   return 0;

}

2: Using unordered_map()

Another way to find duplicate words in C++ is to use the std::unordered_map() function that allows users to count the frequency of each word. We can then iterate over the map and print the words with a count greater than 1.

For example:

#include <iostream>

#include <vector>

#include <unordered_map>

using namespace std;

int main() {

  vector<string> words = {"Black", "Brown", "Yellow", "Black", "Green", "Blue"};

  // Create an unordered_map to store the count of each word

unordered_map<string, int> countMap;

  for (const auto& word : words) {

countMap[word]++;

  }

  // Print the duplicate words

  for (const auto& pair : countMap) {

    if (pair.second > 1) {

cout << pair.first << " ";

    }

  }

  return 0;

}

3: Using set

We can also use a set to find duplicate words. We can insert each word in the set and if the word is already present in the set, it means it is a duplicate.

For example:

#include <iostream>

#include <vector>

#include <set>

using namespace std;

int main() {

  vector<string> words = {"Black", "Brown", "Yellow", "Black", "Green", "Blue"};

  // Create a set to store the duplicate words

  set<string> duplicates;

  set<string> seen;

  // Check for duplicates

  for (const auto& word : words) {

    if (seen.count(word)) {

duplicates.insert(word);

    }

seen.insert(word);

  }

  // Print the duplicate words

  for (const auto& word : duplicates) {

    cout << word << " ";

  }

  return 0;

}

Conclusion

Finding and printing duplicate words in a std::vector<string> using STL functions is an easy task in C++. There are multiple ways to accomplish this, including using std::sort() and std::unique(), std::unordered_map(), and std::set. With the help of these functions, identifying duplicate words in a vector can be done efficiently and accurately, saving time and effort.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.