Open In App

Remove Duplicate/Repeated words from String

Last Updated : 19 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




// C++ program to remove duplicate
// word from string
#include <bits/stdc++.h>
using namespace std;
 
void removeDupWord(string str)
{
    // Used to split string around spaces.
    istringstream ss(str);
 
    // To store individual visited words
    unordered_set<string> hsh;
 
    // Traverse through all words
    do {
        string word;
        ss >> word;
 
        // If current word is not seen before.
        while (hsh.find(word) == hsh.end()) {
            cout << word << " ";
            hsh.insert(word);
        }
 
    } while (ss);
}
 
// Driver function
int main()
{
    string str = "Geeks for Geeks A Computer"
                 " Science portal for Geeks";
    removeDupWord(str);
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
public class Main {
 
  static void removeDupWord(String str)
  {
 
    // Used to split string around spaces.
    StringTokenizer st = new StringTokenizer(str);
 
    // To store individual visited words
    Set<String> hsh = new HashSet<>();
 
    // Traverse through all words
    while (st.hasMoreTokens()) {
      String word = st.nextToken();
 
      // If current word is not seen before.
      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);
  }
}
 
// This code is contributed by codebraxnzt


Python3




def remove_dup_word(string):
    # Used to split string around spaces.
    words = string.split()
     
    # To store individual visited words
    hsh = set()
 
    # Traverse through all words
    for word in words:
        # If current word is not seen before.
        if word not in hsh:
            print(word, end=" ")
            hsh.add(word)
 
# Driver function
if __name__ == '__main__':
    string = "Geeks for Geeks A Computer Science portal for Geeks"
    remove_dup_word(string)


C#




// C# program to remove duplicate
// word from string
using System;
using System.Collections.Generic;
using System.Linq;
 
class Program
{
    static void RemoveDupWord(string str)
    {
        // Split the string into
        // individual words.
        string[] words = str.Split(' ');
        // To store individual visited words
        HashSet<string> hashSet = new HashSet<string>();
        // Traverse through all words
        foreach (string word in words)
        {
            // If current word is not seen before.
            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




// JavaScript program to remove duplicate
// word from string
 
function removeDupWord(string) {
  // Used to split string around spaces.
  const words = string.split(" ");
   
  // To store individual visited words
  const hsh = new Set();
 
  // Traverse through all words
  for (const word of words) {
    // If current word is not seen before.
    if (!hsh.has(word)) {
      process.stdout.write(word + " ");
      hsh.add(word);
    }
  }
}
 
// Driver function
const string = "Geeks for Geeks A Computer Science portal for Geeks";
removeDupWord(string);
 
// Contributed by adityasharmadev01


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
Previous
Next
Share your thoughts in the comments

Similar Reads