Remove Duplicate/Repeated words from String
Last Updated :
19 Sep, 2023
Given a string S, the task is to remove all duplicate/repeated words from the given string.
Examples:
Input: S = “Geeks for Geeks A Computer Science portal for Geeks”
Output: Geeks for A Computer Science portal
Explanation: here ‘Geeks’ and ‘for’ are duplicate so these words are removed from the string
Input: S = “Publish your own articles on GeeksforGeeks and share your knowledge with the world”
Output: Publish your own articles on GeeksforGeeks and share knowledge with the world
Explanation: here ‘your’ is the duplicate word so that word is removed from string
Remove Duplicate/Repeated words from String using Hashing:
Create an empty hash table. Then split given string around spaces. For every word, first check if it is in hash table or not. If not found in hash table, print it and store in the hash table.
- Split the input string into individual words.
- Keep track of visited words using an unordered set.
- Print each word only if it hasn’t been encountered before, effectively removing duplicates from the output.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
void removeDupWord(string str)
{
istringstream ss(str);
unordered_set<string> hsh;
do {
string word;
ss >> word;
while (hsh.find(word) == hsh.end()) {
cout << word << " " ;
hsh.insert(word);
}
} while (ss);
}
int main()
{
string str = "Geeks for Geeks A Computer"
" Science portal for Geeks" ;
removeDupWord(str);
return 0;
}
|
Java
import java.util.*;
public class Main {
static void removeDupWord(String str)
{
StringTokenizer st = new StringTokenizer(str);
Set<String> hsh = new HashSet<>();
while (st.hasMoreTokens()) {
String word = st.nextToken();
while (!hsh.contains(word)) {
System.out.print(word + " " );
hsh.add(word);
}
}
}
public static void main(String[] args) {
String str = "Geeks for Geeks A Computer"
+ " Science portal for Geeks" ;
removeDupWord(str);
}
}
|
Python3
def remove_dup_word(string):
words = string.split()
hsh = set ()
for word in words:
if word not in hsh:
print (word, end = " " )
hsh.add(word)
if __name__ = = '__main__' :
string = "Geeks for Geeks A Computer Science portal for Geeks"
remove_dup_word(string)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void RemoveDupWord( string str)
{
string [] words = str.Split( ' ' );
HashSet< string > hashSet = new HashSet< string >();
foreach ( string word in words)
{
if (!hashSet.Contains(word))
{
Console.Write(word + " " );
hashSet.Add(word);
}
}
}
static void Main( string [] args)
{
string str = "Geeks for Geeks A Computer " +
"Science portal for Geeks" ;
RemoveDupWord(str);
}
}
|
Javascript
function removeDupWord(string) {
const words = string.split( " " );
const hsh = new Set();
for (const word of words) {
if (!hsh.has(word)) {
process.stdout.write(word + " " );
hsh.add(word);
}
}
}
const string = "Geeks for Geeks A Computer Science portal for Geeks" ;
removeDupWord(string);
|
Output
Geeks for A Computer Science portal
Time Complexity: O(n), where n is the number of words in the input string
Auxiliary Space: O(n), because the set ‘hsh’ set can potentially store all the unique words in the input string.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...