Java program to Count Number of Duplicate Words in String

In this post, we will write a simple Java program to count a number of duplicate words in a given string.
In this program, please refer the comments, each line of code is explained via comments.

Java Program to Count Number of Duplicate Words in Given String

import java.util.HashMap;

import java.util.Map;
import java.util.Set;

 * Java program to count number of duplicate words in given string
 * @author
public class DuplicateWordsInString {
    private static void duplicateWords(String inputString) {
        // Splitting inputString into words
        final String[] words = inputString.split(" ");

        // Creating one HashMap with words as key and their count as value
        final Map < String, Integer > wordCount = new HashMap < String, Integer > ();

        // Checking each word
        for (String word: words) {
            // whether it is present in wordCount
            if (wordCount.containsKey(word)) {
                // If it is present, incrementing it's count by 1
                wordCount.put(word, wordCount.get(word) + 1);
            } else {
                // If it is not present, put that word into wordCount with 1 as
                // it's value
                wordCount.put(word, 1);

        // Extracting all keys of wordCount
        final Set < String > wordsInString = wordCount.keySet();

        // Iterating through all words in wordCount

        for (String word: wordsInString) {
            // if word count is greater than 1

            if (wordCount.get(word) > 1) {
                // Printing that word and it's count
                System.out.println(word + " : " + wordCount.get(word));

    public static void main(String[] args) {

        duplicateWords("java guides java");

        duplicateWords("Java is java again java");

        duplicateWords("Super Man Bat Man Spider Man");
java : 2
java : 2
man : 2

Let's analysis and understand the above program:
1. Used split() method to split input String into words.
2. We used HashMap to store key, value pair that is a word with its count. 
final Map<String, Integer> wordCount = new HashMap<String, Integer>();
3. Used containsKey method of HashMap to check whether the word present or not. If HashMap contains word then increment its value by 1 and If a word is not present, put that word as key and value as 1.

4. Finally, iterate over HashMap keyset and check with each key's value, if it is greater than 1 then it is a duplicate word.

Related String Programs

Note that these programs are asked in interviews.

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours

Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course


  1. for duplicateWords("Super Man Bat Man Spider Man");
    the output should be 3
    but it is coming as 2
    can you check.
    word.toLowerCase() is causing issue

    1. You are correct. Due to toLowerCase() the result was calculating wrong. Fixed it. Thanks for reporting.

  2. Where exactly on the program are supposed to to put the toLowerCase() ?


Post a Comment