Find and print duplicate words in std::vector<string> using STL functions

Consider an array of string and find duplicate words in that array and print that duplicate words if exist.

Examples:

Input : { "welcome", "to", "geeks", "for", "geeks" }
Output : geeks

Input : { "reduce", "reuse", "recycle", "reduce", 
          "reuse", "recycle", " recycle" }
Output : recycle
reduce
reuse

Input : { "Go", "Green" }
Output : No Duplicate words



Method 1 (Using Sorting)
1. Sort array of string.
2. compare adjacent word in array of string.
3. if two word are same then push that word in another vector string.
4. print the duplicate words if exist.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find duplicate word in a
// vector<string>
#include<bits/stdc++.h>
using namespace std;
  
void printDuplicates(vector<string> words)
{
    vector<string>duplicate;
  
    // STL function to sort the array of string
    sort(words.begin(), words.end());
  
    for (int i = 1; i<words.size(); i++)
        if (words[i-1] == words[i])
  
            // STL function to push the duplicate
            // words in a new vector string
            duplicate.push_back(words[i]);
  
    int i = 0;
    for (i = 0; i<duplicate.size(); i++)
            cout << duplicate[i] << endl;
  
    if (i == 0)
        cout << "No Duplicate words" << endl;
}
  
// Driver code
int main()
{
    vector<string>words{ "welcome", "to", "geeks",
                                  "for", "geeks" };
    printDuplicates(words);
    return 0;
}

chevron_right


Output:

geeks

 

Method 2 (Using Hashing)
1. Create an empty hash table.
2. One by one traverse words.
3. For every word check if already exists in the hash.
……..if (already exists in hash)
………….Print the word
……..Else
………….Insert the word in hash.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find duplicate word in a
// vector<string>
#include<bits/stdc++.h>
using namespace std;
  
void printDuplicates(vector<string> words)
{
    unordered_set<string> s;
  
    bool dupFound = false;
    for (int i = 1; i<words.size(); i++)
    {
        if (s.find(words[i]) != s.end())
        {
            cout << words[i] << endl;
            dupFound = true;
        }
        else
            s.insert(words[i]);
    }
  
    if (dupFound == false)
        cout << "No Duplicate words" << endl;
}
  
// Driver code
int main()
{
    vector<string>words{ "welcome", "to", "geeks",
                                  "for", "geeks" };
    printDuplicates(words);
    return 0;
}

chevron_right


Output:

geeks

This article is contributed by Rishabh Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.