Java program to find the duplicate characters in a string

In this program, we need to find the duplicate characters in the string.

Great responsibility

To find the duplicate character from the string, we count the occurrence of each character in the string. If count is greater than 1, it implies that a character has a duplicate entry in the string. In above example, the characters highlighted in green are duplicate characters.

ALGORITHM

  • STEP 1: START
  • STEP 2: DEFINE String string1 = “Great responsibility”
  • STEP 3: DEFINE count
  • STEP 4: CONVERT string1 into char string[].
  • STEP 5: PRINT “Duplicate characters in a given string:”
  • STEP 6: SET i = 0. REPEAT STEP 7 to STEP 11 UNTIL i
  • STEP 7: SET count =1
  • STEP 8: SET j = i+1. REPEAT STEP 8 to STEP 10 UNTIL j
  • STEP 9: IF (string[i] == string[j] && string[i] != ‘ ‘)
                  then
                  count = count + 1
                  string[j]= 0
  • STEP 10: j = j + 1
  • STEP 11: i = i + 1
  • STEP 12: IF(count>1 && string[i] != 0) then PRINT string[i]
  • STEP 13: END

Program:

  1. public class DuplicateCharacters {    
  2.      public static void main(String[] args) {    
  3.         String string1 = “Great responsibility”;    
  4.         int count;    
  5.             
  6.         //Converts given string into character array    
  7.         char string[] = string1.toCharArray();    
  8.             
  9.         System.out.println(“Duplicate characters in a given string: “);    
  10.         //Counts each character present in the string    
  11.         for(int i = 0; i <string.length; i++) {    
  12.             count = 1;    
  13.             for(int j = i+1; j <string.length; j++) {    
  14.                 if(string[i] == string[j] && string[i] != ‘ ‘) {    
  15.                     count++;    
  16.                     //Set string[j] to 0 to avoid printing visited character    
  17.                     string[j] = ‘0’;    
  18.                 }    
  19.             }    
  20.             //A character is considered as duplicate if count is greater than 1    
  21.             if(count > 1 && string[i] != ‘0’)    
  22.                 System.out.println(string[i]);    
  23.         }    
  24.     }    
  25. }     

Output:

Duplicate characters in a given string:
r
e
t
s
i

Leave a Comment