find duplicate Characters in a String

Java Program to find duplicate Characters in a String with Example

This program would detect duplicate characters in a string and display the number of them.

duplicate Characters duplicate Characters duplicate Characters duplicate Characters vduplicate Characters duplicate Characters

public class Details {
 
  public void countDupChars(String str){
 
    //Create a HashMap 
    Map<Character, Integer> map = new HashMap<Character, Integer>(); 
 
    //Convert the String to char array
    char[] chars = str.toCharArray();
 
    /* logic: char are inserted as keys and their count
     * as values. If map contains the char already then
     * increase the value by 1
     */
    for(Character ch:chars){
      if(map.containsKey(ch)){
         map.put(ch, map.get(ch)+1);
      } else {
         map.put(ch, 1);
        }
    }
 
    //Obtaining set of keys
    Set<Character> keys = map.keySet();
 
    /* Display count of chars if it is
     * greater than 1. All duplicate chars would be 
     * having value greater than 1.
     */
    for(Character ch:keys){
      if(map.get(ch) > 1){
        System.out.println("Char "+ch+" "+map.get(ch));
      }
    }
  }
 
  public static void main(String a[]){
    Details obj = new Details();
    System.out.println("String: developersdome.com");
    System.out.println("-------------------------");
    obj.countDupChars("developersdome.com");
  
    System.out.println("\nString: SagarPaliwal");
    System.out.println("-------------------------");
    obj.countDupChars("SagarPaliwal");
 
    System.out.println("\nString: #@$@!#$%!!%@");
    System.out.println("-------------------------");
    obj.countDupChars("#@$@!#$%!!%@");
  }
}

Output:

String: developersdome.com
Char e 4
Char d 2
Char m 2
Char o 3
String: Sagarpaliwal
Char S 1
Char a 4
Char p 1
Char i 1
String: #@$@!#$%!!%@
Char # 2
Char ! 3
Char @ 3
Char $ 2
Char % 2

Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today.

convert char to String and String to char convert char to String and String to char convert char to String and String to char convert char to String and String to char convert char to String and String to char v convert char to String and String to char convert char to String and String to char Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today.

This Post Has One Comment

Leave a Reply