3 ways: How to Find Duplicate Words in String in Java

In this tutorial, I will be sharing how to find duplicate words in String in Java. There are many ways to solve this problem. One easy way is by using the Collections.frequency() method. Another way is without using the Collections API. I will be sharing both of them. Let's dive deep into the topic:

Read Also:  String Interview Questions and Answers 

First, understand the problem statement by writing some examples:

Examples of Duplicate Words in String are:

Given String: "Java is a programming language. Python is also a programming language."
Duplicate words are : "is","a","programming","language." 
 
Given String: "Java has 51 keywords in total. Null, true, and false might seem like keywords but they are not in Java"  
Duplicate words are : "keywords", "in", "java"  

Java Program to find Duplicate Words in String


1. Using HashSet

In the below program I have used HashSet and ArrayList to find duplicate words in String in Java.

import java.util.*; 
 
public class JavaHungry {
    public static void main(String args[]) {
      // Given String containing duplicate words
      String input = "Java is a programming language. Python is also a programming language.";
      // Converting given String to lowerCase
      input = input.toLowerCase();
      /* Split the Input String into words using 
      built-in split() method */
      String[] strArray = input.split(" ");
      /* Declare List of String that will 
      contain repeated words*/
      List<String> repeatedWords = new ArrayList<>();
      /* Declare HashSet of String that will 
      contain unique words */
      HashSet<String> uniqueWords = new HashSet<>();
      for(String str : strArray)
      {
          if (!uniqueWords.add(str))
            repeatedWords.add(str);
      }
      System.out.println(repeatedWords);
    }
}

Output:
[is, a, programming, language.]

The above program can be much simpler by using a built-in Collections.frequency() method. We will see in the next step.

2. Using Collections.frequency() method

In the below program I have used Collections.frequency() method.

import java.util.*;
public class JavaHungry {
    public static void main(String args[]) {
      // Given Input String containing duplicate words
      String input = "Alive is Awesome. Be in present. Manchester United is also known as RedDevil";
      // Converting given Input String to lowerCase
      input = input.toLowerCase();
      /* Split the given Input String into words using 
      built-in split() method */
      String[] strArray = input.split(" ");
      // Converting String array to List of String
      List<String> listOfWords = Arrays.asList(strArray);
      /* Declare HashSet of String that will 
      contain unique words */
      HashSet<String> uniqueWords = new HashSet<>(listOfWords);
      for(String word : uniqueWords)
      {
          if(Collections.frequency(listOfWords,word) > 1)
            System.out.print(" "+ word+" ");
      }
    }
}

Output:
 is

3. Without Using Collections

In the below program we have printed the duplicate words in String without using Collections API.

public class JavaHungry {
    public static void main(String args[]) {
      // Given below Input String containing duplicate words
      String input = "Google is the most popular search engine in the world. Bing comes at number two.";
      // Converting given  Input String to lowerCase below:
      input = input.toLowerCase();
      /* Split the given Input String into words using 
      built-in split() method below*/
      String[] words = input.split(" ");
      int length = words.length;
      // Using for loop 
      for( int i=0; i < length; i++)
      {
          // Set count to 1 for every iteration
          int count = 1;
          for(int j=i+1; j < length; j++)
          {
              if(words[i].equals(words[j]))
              {
                  count++;
                  /* Setting words[j]="0" to avoid 
                  visited words */
                  words[j] = "0";
              }
          }
          if (words[i] != "0" && count > 1)
              System.out.print(" "+words[i]+" ");
      }
    }
}

Output:
the


That's all for today, please mention in comments in case you know any other method to find duplicate words in a String in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry