Count repeating words Program in java

Question:

In a given sentence, find the maximum repeated word and print the same.(Assume that all the characters are in lower case)

Sample Input 1:

java is programming language and an object oriented language

Sample Output1:

language

Sample Input2:

suvi felt happy because suvi saw that the others were happy

Sample Output2:

suvi

happy

Sample Input2:

he went out yesterday

Sample Output2:

No repetition of words

CODE:

import java.util.*;
public class Main {
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        String str[]=sc.nextLine().split(" ");
        
        int len=str.length;
        int flag=0;
        for(int i=0;i<len;i++)
        {
            for(int j=(i+1);j<len;j++)
            {
                if((str[j]).equals(str[i]))
                {
                    System.out.println(str[j]);
                    flag++;
                    break;
                }
            }
        }
        if(flag==0)
        {
            System.out.println("No repetition of words");
        }
    }
}
Next:
  1. Sentence – Convert to upper and lower
  2. Count consecutive repeating characters
  3. Zig zag Array

Leave a Comment

Scroll to Top