1. Introduction

Finding duplicate words in a string is a common task in text processing and Java programming. This task involves identifying and possibly counting the words that appear more than once in a given string. In this blog post, we will look at a simple Java program that finds and prints the duplicate words in a string.

2. Program Steps

1. Define a main method to serve as the entry point of the program.

2. Initialize a string variable with the text in which we want to find duplicate words.

3. Convert the string to lowercase to make the comparison case-insensitive.

4. Split the string into an array of words.

5. Create a HashMap to hold the words and their occurrences.

6. Loop through the array of words, and for each word, check if it is already in the map. If it is, increment its count, otherwise add it to the map with count 1.

7. After populating the map, loop through it and print the words that have a count greater than 1, which are the duplicate words.

3. Code Program

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

public class DuplicateWords {

    public static void main(String[] args) {
        // Step 2: Initialize a string variable
        String text = "Java is a versatile programming language and Java is used worldwide";

        // Step 3: Convert the string to lowercase
        text = text.toLowerCase();

        // Step 4: Split the string into an array of words
        String[] words = text.split("\\s+");

        // Step 5: Create a HashMap to hold the words and their occurrences
        Map<String, Integer> wordCount = new HashMap<>();

        // Step 6: Loop through the array of words and populate the map
        for (String word : words) {
            wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
        }

        // Step 7: Loop through the map and print the duplicate words
        System.out.println("Duplicate words in the string:");
        for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
            if (entry.getValue() > 1) {
                System.out.println(entry.getKey());
            }
        }
    }
}

Output:

Duplicate words in the string:
java
is

4. Step By Step Explanation

Step 1: The main method is defined, serving as the starting point of the program.

Step 2: A string variable text is initialized with the text where we want to find duplicate words.

Step 3: The string text is converted to lowercase to ensure the comparison is case-insensitive.

Step 4: The string is split into an array of words using the split method with spaces as the delimiter.

Step 5: A HashMap named wordCount is created to hold the words and their occurrences.

Step 6: We loop through the array of words. For each word, we check if it is already in the map and update its count.

Step 7: Finally, we loop through the wordCount map and print the words that have a count greater than 1, indicating that they are duplicate words.

This program efficiently finds and prints the duplicate words in a given string using a HashMap in Java.