Java program to find the duplicate words in a string

In this program, we need to find out the duplicate words present in the string and display those words.

Example: big black bug bit a big black dog on his big black nose

To find the duplicate words from the string, we first split the string into words. We count the occurrence of each word in the string. If count is greater than 1, it implies that a word is duplicate in the string.

ALGORITHM

  • STEP 1: START
  • STEP 2: DEFINE String string = “Big black bug bit a big black dog on his big black nose”
  • STEP 3: DEFINE count
  • STEP 4: CONVERT string into lower-case.
  • STEP 5: INITIALIZE words[] to SPLIT the string.
  • STEP 6: PRINT “Duplicate words in a given string:”
  • STEP 7: SET i=0. REPEAT STEP 8 to 12 STEP UNTIL i
  • STEP 8: SET count =1.
  • STEP 9: SET j = i+1. REPEAT STEP 10 to STEP 11 UNTIL j
  • STEP 10: IF (words[i].equals(words[j])
                  then
                  count = count + 1
                  words[j]= 0
  • STEP 11: j = j + 1
  • STEP 12: i = i + 1
  • STEP 13: IF(count>1 && words[i] != 0) then PRINT words[i]
  • STEP 13: END

Program:

  1. public class DuplicateWord {    
  2.     public static void main(String[] args) {    
  3.         String string = “Big black bug bit a big black dog on his big black nose”;    
  4.         int count;    
  5.             
  6.         //Converts the string into lowercase    
  7.         string = string.toLowerCase();    
  8.             
  9.         //Split the string into words using built-in function    
  10.         String words[] = string.split(” “);    
  11.             
  12.         System.out.println(“Duplicate words in a given string : “);     
  13.         for(int i = 0; i < words.length; i++) {    
  14.             count = 1;    
  15.             for(int j = i+1; j < words.length; j++) {    
  16.                 if(words[i].equals(words[j])) {    
  17.                     count++;    
  18.                     //Set words[j] to 0 to avoid printing visited word    
  19.                     words[j] = “0”;    
  20.                 }    
  21.             }    
  22.                 
  23.             //Displays the duplicate word if count is greater than 1    
  24.             if(count > 1 && words[i] != “0”)    
  25.                 System.out.println(words[i]);    
  26.         }    
  27.     }    
  28. }    

Output:

Duplicate words in a given string :
big
black

Leave a Comment