Remove Duplicate words in a sentence using Java

This program is used to remove words which are repeated in a sentence in order to reduce the duplicate words and reduce the sentence length.
Duplicate words add redundancy to the sentence and can alter the meaning of the sentence.
Hence they should be removed.

This program is purely to remove the visible duplicates present in a sentence, and not to count the duplicates.
If you want to count or find the duplicates in a sentence, you can refer to the following link:
Checking for Duplicacy in an array using Hashing Technique in Java

Now, let’s figure out how to remove these duplicate words.
The following program is strictly character sensitive i.e make sure to properly define lower case and upper case characters.

Giving the right input containing duplicate words/numbers

  1. In the following code, we can input the sentence containing any characters, numbers etc.
  2. Make sure that if we want to remove the duplicates they must be exactly the same. For example, if there is a name like “Rahul” in the sentence and you want to remove the duplicate make sure the duplicate word is “Rahul” and not “rahul” since it is character sensitive.
  3. We can remove duplicate numbers in the sentence as well.

Program:

import java.io.*;
class RemoveDuplicates
{
  public static void main(String[] args)throws IOException
  {	
    String input=""; 		//String to be inputted according to user	
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the sentence: ");
    input= br.readLine();
    String[] words=input.split(" ");    	//Splitting every word in the sentence with the help of spaces
    for(int i=0;i<words.length;i++)		//Outer loop for Comparison and to check if sentence is empty or not		
    {
      if(words[i]!=null)
      {
      
      for(int j=i+1;j<words.length;j++)	        //Inner loop to compare two words in a sentence to check for duplicacy
      {
        
      if(words[i].equals(words[j]))	        //Checking if both the compared strings are equal
        {
          words[j]=null;			//Deletes the duplicate words if the compared strings are equal
        }
      }
      }
    }
    for(int k=0;k<words.length;k++)		//Displaying the String without the duplicate words	
    {
      if(words[k]!=null)
      {
        System.out.print(words[k] + " ");
      }
      
    }  
  }
}

As seen above it is clearly explained how to remove the duplicate words in a sentence.
Please see the comments as it makes the code easier to understand and implement.
The following code gives the following output.

Output:

Enter the sentence: 
Welcome to this program this program where we remove duplicate words words
The string without duplicate words is: 
Welcome to this program where we remove duplicate words

As seen above, we have successfully removed duplicate words from a sentence

Leave a Reply

Your email address will not be published. Required fields are marked *