How To Count Repeated Words in a String in Java

Hello Friends, Today in this tutorial we will learn ‘How to count Repeated Words in a string’

Using help of HashMap class which is part of Map interface. It stores the object in key and value pair format.

package JavaPkg;
import java.util.*;


public class FindRepeatedWords {
    
    public static void main(String[] args) {
        
        String s1= "I am learning learning PHP PHP PHP Programming";
        
        Map<String,Integer> m = new HashMap<String, Integer>();
        Integer count=1;
        String[] aa= s1.split(" ");
        for(int i=0; i<aa.length; i++) 
        {
            
            if(!m.containsKey(aa[i])) {
                
                m.put(aa[i],count );
            }else 
            {
                m.put(aa[i], m.get(aa[i])+1);
                
            }
            
        }
        
        for(String x : m.keySet()) {
            
            System.out.println("The count of word: " + x +"-->"+ m.get(x));
        }
        

    }

}

Output-

How to count repeated words in String in Java

Explanation: In above program, I have taken one string in s1 reference variable. Here we will have to print word and their count so I will use Map interface, which stores any object into Key and value form. In Key, we will store word and in value, we will store the number of occurrence of word.

Therefore, we will declare Map with String and Integer types.

First, we will split the string and store into an array. Apply for loop to iterate the each word one by one. Apply if condition where we check the word on particular index is present in Map or not. If it is not present then it will go inside if condition and add that word into Map with count 1 (Initially I have initialized count value 1). If condition is false means value is already in Map then we will increase the value with plus one (Using get() method , get the count of Map key’s value).

Now apply for each loop to find the Keyset (it is a method to find the Key value pair of the Map)

Once we get this KeySet, we have to print the value.


How To Print only repeated words without occurrence?

In above program, after for each loop, we have to check one condition like if map key value is greater >1 then print only that key value.

Program-

package JavaPkg;
import java.util.*;


public class FindRepeatedWords {
    
    public static void main(String[] args) {
        
        String s1= "I am learning learning PHP PHP PHP Programming";
        
        Map<String,Integer> m = new HashMap<String, Integer>();
        Integer count=1;
        String[] aa= s1.split(" ");
        for(int i=0; i<aa.length; i++) 
        {
            
            if(!m.containsKey(aa[i])) {
                
                m.put(aa[i],count );
            }else 
            {
                m.put(aa[i], m.get(aa[i])+1);
                
            }
            
        }
        System.out.println("Duplicate words:" );
        for(String x : m.keySet()) {
            
            if(m.get(x)> 1) {
                
                System.out.println( x );
                
            }
        }

    }

}

Output-

How to print only repeated words in a String

 

 

I hope you have enjoyed this tutorial. Happy Learning 🙂

Leave a Comment