Java Program to Find Repeated Words in a String

The following Java program prints repeated/duplicated words in a String. The program uses case insensitive comparison (For example, program assumes words CAT, cat and Cat etc. are all same). This algorithm is useful in text processing programs where word frequency calculations are needed.

The program first asks for the input string from the command line. Using the split function, this string is broken into individual words. We then add each word into a map checking whether the word already exists. Whenever we find a word that is repeated, we print the word.

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

// How to find repeated/duplicated words in a string using Java
public class DuplicateWords {
    
    public static void main(String[] args) {
        System.out.print("Enter string to analyse:");
        Scanner sn = new Scanner(System.in);
        String input = sn.nextLine();
        
        // first let us split string into words
        String[] words = input.split(" ");
        
        // adds all words into a map
        // we also check whether the word is already in map!
        Map<String,String> wordMap = new HashMap<String,String>();
        for(int i=0;i<words.length;i++) {
            String word = words[i].toUpperCase(); // for case insensitive comparison
            if(wordMap.get(word)!=null) {
                // we found a duplicated word!
                System.out.println("Duplicated/Repeated word:"+word);
            }else {
                wordMap.put(word, word);
            }
        }
    }   
}

If a word is repeated more than once, it is printed multiple times. The following program fixes this by keeping track of printed words using another map.

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

// How to find repeated/duplicated words in a string using Java
public class DuplicateWords2 {
    
    public static void main(String[] args) {
        System.out.print("Enter string to analyse:");
        Scanner sn = new Scanner(System.in);
        String input = sn.nextLine();
        
        // first let us split string into words
        String[] words = input.split(" ");
        
        // adds all words into a map
        // we also check whether the word is already in map!
        Map<String,String> wordMap = new HashMap<String,String>();                
        Map<String,String> printedMap = new HashMap<String,String>();

        for(int i=0;i<words.length;i++) {
            String word = words[i].toUpperCase(); // for case insensitive comparison
            if(wordMap.get(word)!=null) {
                // we found a duplicated word!
                if(printedMap.get(word)==null) { // first check if it is printed already!
                    System.out.println("Duplicated/Repeated word:"+word);
                    printedMap.put(word, word); 
                }
            }else {
                wordMap.put(word, word);
            }
        }
    }
    
}

Do you have a programming problem that you are unable to solve? Please send us your problem and we will publish the solution on this site within 48 hours! Please email us.


Leave a Reply