Java Program To Remove Duplicate Words In A String – 3 Ways

In this post, We will discuss the Java program to remove duplicate words in a String. We have given 3 approaches to perform this operation.

Let’s see all the scenarios to remove specific words in a String.

  1. Java Program To Remove Duplicate Words In A String Using For Loop
  2. Java Program To Remove Duplicate Words In A String Using LinkedHashSet
  3. Java Program To Remove Duplicate Words In A String Using regex.

Read Also: How To Reverse A String In Java – Learn 5 Easy Methods

Approach-1: Java program to remove duplicate words in a String using for loop

In this approach, we will use for loop to remove duplicate words from a String. First, we will remove duplicates words, and then we will display the given sentence without duplication.

Let’s see the program using for loop here.

Java program to remove duplicate words using for loop:

/**
 * Java Program To Remove Duplicate Words In A String Using For Loop
 */
package cf.java.string;

import java.util.Scanner;

public class RemoveDuplicateWords {

	public static void main(String[] args) {
		// Declare all variables
		String sentence, result = "";
		String allWords[];
		
		Scanner sc = new Scanner(System.in);
		// Accept any sentence from User 
		System.out.print("Enter your sentence: "); 
		sentence = sc.nextLine().toLowerCase();  //convert to lower case
		
		// Split the given sentence to get each word as String array
		allWords = sentence.split(" ");
		// Use for loop to remove duplicate words
		for(int i=0; i<allWords.length; i++) {
			for(int j=i+1; j<allWords.length; j++) {
				if(allWords[i].equals(allWords[j])) {
					allWords[j] = "remove";
				}
			}
		}
		// Convert to String
		for(String word: allWords) {
			if(word != "remove") {
				result = result + word + " ";
			}
		}
		// Display given String after removing duplicates
		System.out.println("\nSentence after removing duplicate words: " + result);

	}

}Code language: JavaScript (javascript)

Output:

Enter your sentence: codingface is is the online learning platform.
Sentence after removing duplicate words: codingface is the online learning platform.

Explanation of for loop program:

In the above program, we have used the split( ) method to split the given sentence into individual words. We have used 2 for loops to compare each word with the next word and if we will find any duplicate words, then we will replace this word withremove‘. Finally, by excluding the ”remove‘ word, we can display the given sentence without duplicate words.

Read Also: Java Program To Print Vowels In A String – 2 Simple Programs

Approach-2: Java program to remove duplicate words in a String using LinkedHashSet class

In this approach, we will use LinkedHashSet class to remove duplicate words from a String. First, we will remove duplicated words, and then we will display the given sentence without duplication.

Let’s see the program using LinkedHashSet now.

Java program to remove duplicate words using LinkedHashSet class

/**
 * Java Program To Remove Duplicate Words In A String Using LinkedHashSet
 */
package cf.java.string;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Scanner;

public class RemoveDuplicateWordsUsingLinkedHashSet {

	public static void main(String[] args) {
		// Declare all variables
		String sentence, result = "";
		String allWords[];
		
		Scanner sc = new Scanner(System.in);
		// Accept any sentence from User 
		System.out.print("Enter your sentence: "); 
		sentence = sc.nextLine().toLowerCase();  //convert to lower case
		
		// Split the given sentence to get each word as String array
		allWords = sentence.split(" ");
		
		// Convert String Array allWords to LinkedHashSet to remove duplicates
        LinkedHashSet<String> set = new LinkedHashSet<String>( Arrays.asList(allWords) );
        
		// Convert to String
		for(String word: set) {
			result = result + word + " ";
		}
		// Display given String after removing duplicates
		System.out.println("Sentence after removing duplicate words: " + result);

	}

}Code language: JavaScript (javascript)

Output:

Enter your sentence: codingface is is the online learning platform.
Sentence after removing duplicate words: codingface is the online learning platform.

Explanation of above program:

In the above program, we have accepted a sentence from the user. By using the split( ) method, we have converted the given string into a collection of words as a String array.

As we know that Set stores only unique elements, So LinkedHashSet will not store duplicate words. So we have used LinkedHashSet to store the String array. By default, it will remove all duplicate words.

Finally, we have stored each unique word to a new String and then we will display the given sentence without duplicates. 

Read Also: How To Check String Contains Special Characters In Java

Approach-3: Java program to remove duplicate words in a String using regex

In this approach, we will use regex to remove duplicate words from a String. First, we will remove duplicates words, and then we will display the given sentence without duplication.

Required regex = "\\b(\\w+)(?:\\W+\\1\\b)+";Code language: JavaScript (javascript)

Java program to remove duplicate words using regex:

/**
 * Java Program To Remove Duplicate Words In A String Using regex.
 */
package cf.java.string;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RemoveDuplicateWordsUsingRegex {

	public static void main(String[] args) {
		// Declare all variables
		String sentence, regex;
		
		Scanner sc = new Scanner(System.in);
		// Accept any sentence from User 
		System.out.print("Enter your sentence: "); 
		sentence = sc.nextLine();
		// Define regex 
		regex = "\\b(\\w+)(?:\\W+\\1\\b)+";
		// Define pattern to compile regex
		Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
		// Match whether regex matching with sentence or not
		Matcher match = pattern.matcher(sentence);
		// Use while loop to find and replace duplicate words
		while(match.find()) {
			sentence = sentence.replace(match.group(), match.group(1));
		}
		// Display sentence
		System.out.println("Sentence after removing duplicate words: " + sentence);

	}

}Code language: JavaScript (javascript)

Output:

Enter your sentence: codingface is is the online learning platform.
Sentence after removing duplicate words: codingface is the online learning platform.

Explanation of program using regex:

We have taken predefined regex and then by using Pattern and Matcher class, we will match regex with the given sentence.

We have used a while loop to find whether a given sentence contains duplicate words or not. If a given sentence or string contains duplicate words, we will remove them by using replace( ) method.

Finally, we will display the unique sentence or string.

Read Also: How To Find Multiples Of A Float Value In Java?

Other readers also ask for

  • How to remove duplicate words from a sentence using Java?
  • How to remove duplicate words from a String?
  • How to find duplicate words from a sentence?

5 thoughts on “Java Program To Remove Duplicate Words In A String – 3 Ways”

Leave a Comment