Java Program to find duplicate Characters in a String

In this post, we will see how to find duplicate Characters in a String.

Approach:

  1. Create a HashMap and character of String will be inserted as key and its count as value.
  2. If Hashamap already contains char,increase its count by 1, else put char in HashMap
  3. If value of Char is more than 1, that means it is duplicate character in that String

Java Program to find duplicate Characters in a String

When you run above program, you will get below output:

Other String Programs :

Was this post helpful?

Comments

  1. How about using a Set?

    private static boolean hasDupeLetter(String str) {

    if(null == str || str.length() <= 1) { return false; } Set set = new HashSet();
    for(int i = 0; i < str.length(); i++) { set.add(str.charAt(i)); } return str.length() == set.size(); }

Leave a Reply

Your email address will not be published. Required fields are marked *