Java Program to Find Duplicate Words in a String

In this article, we will walk you through a Java program that identifies duplicate words in a given string.

The Strategy

  • Convert the string into an array of words. 
  • Use a map (e.g., HashMap) to maintain the frequency count of each word. 
  • Traverse through the array of words. Update the word count on the map. 
  • After storing the counts, traverse the map to identify words with a frequency greater than one.

Java Program to Find Duplicate Words in a String

import java.util.HashMap;
import java.util.Map;

public class DuplicateWordFinder {

    public static void main(String[] args) {
        String sentence = "Java is a programming language and Java is also a platform";

        System.out.println("Duplicate words in the string are:");
        findDuplicateWords(sentence);
    }

    public static void findDuplicateWords(String str) {
        String[] words = str.split(" ");
        Map<String, Integer> wordCountMap = new HashMap<>();

        for (String word : words) {
            String wordLowerCase = word.toLowerCase();
            wordCountMap.put(wordLowerCase, wordCountMap.getOrDefault(wordLowerCase, 0) + 1);
        }

        for (Map.Entry<String, Integer> entry : wordCountMap.entrySet()) {
            if (entry.getValue() > 1) {
                System.out.println(entry.getKey() + ": " + entry.getValue() + " times");
            }
        }
    }
}

Output:

Duplicate words in the string are:
a: 2 times
java: 2 times
is: 2 times

Step-by-Step Explanation: 

Setting Up the String: 

 String sentence = "Java is a programming language and Java is also a platform";

Here, we've initialized a string named sentence. Our aim is to find duplicate words in this sentence.

Converting the String into an Array of Words:

        String[] words = str.split(" ");

Using the split method of the String class, we can split the string into an array of words based on spaces. 

Initializing the Word Count Map: 

        Map<String, Integer> wordCountMap = new HashMap<>();

We'll use a HashMap to store the frequency count of each word in the string. 

Populating the Map with Word Counts:

        for (String word : words) {
            String wordLowerCase = word.toLowerCase();
            wordCountMap.put(wordLowerCase, wordCountMap.getOrDefault(wordLowerCase, 0) + 1);
        }

We iterate through each word in the word array. We convert the word to lowercase (to ensure the comparison is case-insensitive) and then update its count in the wordCountMap

Identifying and Printing Duplicate Words: 

        for (Map.Entry<String, Integer> entry : wordCountMap.entrySet()) {
            if (entry.getValue() > 1) {
                System.out.println(entry.getKey() + ": " + entry.getValue() + " times");
            }
        }

We then iterate through the map. For each entry, if the count (value) is greater than one, we print the word (key) and its count.

Conclusion

Identifying duplicate words in a string is an essential text analysis task that introduces beginners to string manipulation, arrays, and the use of data structures like HashMap in Java. This guide illustrates a clear and efficient approach to solving this common problem. As you delve deeper into Java and other programming languages, you'll encounter more complex tasks and challenges.

Related Java String Programs with Output

Comments