Algorithm


  • 1. STEP 1: START.
  • 2. STEP 2: DEFINE String string1 = "Great responsibility"
  • 3. STEP 3: DEFINE count.
  • 4. STEP 4: CONVERT string1 into char string[].
  • 5. STEP 5: PRINT "Duplicate characters in a given string:"
  • 6. STEP 6: SET i = 0. REPEAT STEP 7 to STEP 11 UNTIL i.
  • 7. STEP 7: SET count =1.
  • 8. STEP 8: SET j = i+1.

 

Code Examples

#1 Code Example to find duplicate characters in string

Code - Java Programming

public class DuplStr {
 public static void main(String argu[]) {

  String str = "w3schools";
  int cnt = 0;
  char[] inp = str.toCharArray();
  System.out.println("Duplicate Characters are:");
  for (int i = 0; i < str.length(); i++) {
   for (int j = i + 1; j < str.length(); j++) {
    if (inp[i] == inp[j]) {
     System.out.println(inp[j]);
     cnt++;
     break;
    }
   }
  }
 }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Duplicate Characters are: s o
Advertisements

Demonstration


Java programing example is used to find duplicate characters in string-DevsEnv