Find and print duplicate words in std::vector<string> using STL functions Last Updated : 25 Nov, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. CPP // 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 if (duplicate.empty()) duplicate.push_back(words[i]); else if (words[i] != duplicate.back()) duplicate.push_back(words[i]); } } if (duplicate.size() == 0) cout << "No Duplicate words" << endl; else for (int i = 0; i < duplicate.size(); i++) cout << duplicate[i] << endl; } // Driver code int main() { vector<string> words{ "welcome", "to", "geeks", "for", "geeks", "to", "geeks" }; printDuplicates(words); return 0; } Outputgeeks to 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. CPP // 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; } Output: geeks Comment More infoAdvertise with us Next Article Given a sequence of words, print all anagrams together using STL R Rishabh Improve Article Tags : C++ STL Practice Tags : CPPSTL Similar Reads Remove duplicates from a string using STL in C++ Given a string S, remove duplicates in this string using STL in C++ Examples: Input: Geeks for geeks Output: Gefgkors Input: aaaaabbbbbb Output: ab Approach: The consecutive duplicates of the string can be removed using the unique() function provided in STL. Below is the implementation of the above 1 min read Remove duplicates from a string using STL in C++ Given a string S, remove duplicates in this string using STL in C++ Examples: Input: Geeks for geeks Output: Gefgkors Input: aaaaabbbbbb Output: ab Approach: The consecutive duplicates of the string can be removed using the unique() function provided in STL. Below is the implementation of the above 1 min read Given a sequence of words, print all anagrams together using STL Given an array of words, print all anagrams together. For example, Input: array = {âcatâ, âdogâ, âtacâ, âgodâ, âactâ}output: cat tac act, dog godExplanation: cat tac and act are anagrams and dog and god are anagrams as they have the same set of characters.Input: array = {âabcâ, âdefâ, âghiâ}output: 10 min read Calculate the frequency of each word in the given string Given a string str, the task is to find the frequency of each word in a string. Examples: Input: str = "Geeks For Geeks" Output: For 1 Geeks 2 Explanation: For occurs 1 time and Geeks occurs 2 times in the given string str. Input: str = "learning to code is learning to create and innovate" Output: a 6 min read Find the duplicate characters in a string in O(1) space Given a string str, the task is to find all the duplicate characters present in a given string in lexicographical order without using any additional data structure. Examples: Input: str = "geeksforgeeks" Output: e g k s Explanation: Frequency of character 'g' = 2 Frequency of character 'e' = 4 Frequ 10 min read Remove duplicates from a sorted array using STL in C++ Given a sorted array, the task is to remove the duplicate elements from the array using STL in C++ Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: arr[] = {2} Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4, 5} Approach: The duplicates of the array can be removed using the un 2 min read Like