A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/dsa/remove-duplicaterepeated-words-string/ below:

Remove Duplicate/Repeated words from String

Remove Duplicate/Repeated words from String

Last Updated : 11 Jul, 2025

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 

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.

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.


Remove Duplicate/Repeated words from String


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4