Given a string, we have to remove all the duplicate word from the string . We can use unordered set to store words and then check each word to see if it is already present in the set.

Below is the implementation

include<iostream> #include<sstream> #include<unordered_set> using namespace std; void remDuplicateWord(string str) { // Used to split string around spaces. stringstream ss(str); // To store individual visited words unordered_set<string> hash_tab; // Traverse through all words in the string do { string word; ss >> word; // check if current word already exist,if not then insert while (hash_tab.find(word) == hash_tab.end()) { cout << word << " "; hash_tab.insert(word); } } while (ss); } // Driver function int main() { string str = "learn and learn again and again"; remDuplicateWord(str); return 0; }