
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find and print duplicate words in std::vector using STL functions using C++.
Consider we have a list of strings. The list has some duplicate strings. We have to check which strings are occurred more than once. Suppose the string list is like [“Hello”, “Kite”, “Hello”, “C++”, “Tom”, “C++”]
Here we will use the hashing technique, so create an empty hash table, then traverse each string, and for each string, s is already present in the hash, then display the string, otherwise insert into the hash.
Example
#include<iostream> #include<vector> #include<unordered_set> using namespace std; void displayDupliateStrings(vector<string> strings) { unordered_set<string> s; bool hasDuplicate = false; for (int i = 0; i<strings.size(); i++) { if (s.find(strings[i]) != s.end()) { cout << strings[i] << endl; hasDuplicate = true; } else s.insert(strings[i]); } if (!hasDuplicate) cout << "No Duplicate string has found" << endl; } int main() { vector<string>strings{"Hello", "Kite", "Hello", "C++", "Tom", "C++"}; displayDupliateStrings(strings); }
Output
Hello C++
- Related Articles
- std::vector::resize() vs. std::vector::reserve() in C++
- Difference between std::vector and std::array in C++
- How to reverse a Vector using STL in C++?
- How to find the maximum element of a Vector using STL in C++?
- How to find the sum of elements of a Vector using STL in C++?
- Sorting a vector of custom objects using C++ STL
- Quickly merging two sorted arrays using std::merge() in C++ STL(cute ho ap)
- Computing index using pointers returned by STL functions in C++
- vector::begin() and vector::end() in C++ STL
- How to print duplicate characters in a String using C#?
- How to sort a Vector in descending order using STL in C++?
- Print prime numbers in a given range using C++ STL
- Python program to print Possible Words using given characters
- How to shuffle a std::vector in C++
- Program to print prime numbers in a given range using C++ STL

Advertisements