Java Program to find all duplicate characters in a string



Java Program to find all duplicate characters in a string

Sample Code

import java.util.*;

public class dupl_char {
  public static void main(String[] args) {
    System.out.println("Enter the String");
    Scanner sc = new Scanner(System.in);
    String string1 = sc.nextLine();
    int count;
    char string[] = string1.toCharArray();
    System.out.println("Duplicate characters in a given string: ");
    for (int i = 0; i < string.length; i++) {
      count = 1;
      for (int j = i + 1; j < string.length; j++) {
        if (string[i] == string[j] && string[i] != ' ') {
          count++;
          string[j] = '0';
        }
      }
      if (count > 1 && string[i] != '0')
        System.out.println(string[i]);
    }
  }
}

Output

Enter the String
Nandakishore
Duplicate characters in a given string: 
a

Related Searches to Java Program to find all duplicate characters in a string