How to find Duplicate characters in given String in Java?

Problem Description :

Write a program in Java that prints all duplicate characters in a String. For example  "Java" has one duplicate character 'a'.

Algorithm :

We are using below steps for creating Java program.
  1. Convert original String to character Array.
  2. Remove all duplicate characters from String by converting String to Set.
  3. Get new String by converting Set (from Step 2) to String.
  4. Convert String (from Step 3) to character Array.
  5. If original String and new String are same then there is no duplicate character in original String otherwise go to Step 6.
  6. One by one check all characters of new String with Original String. If count > 1 it means duplicate characters present in String.

Java Program :

Output :

Case 1 :

Output of Java program that prints duplicate characters in given string - Case1
Output - Duplicate characters present in given String.

Case 2 :

Output of Java program that prints duplicate characters in given string - Case2
Output - Duplicate characters not present in given String.

References :

Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.

Tags : Interview questions on String, Solution in Java, Duplicate characters, Iterator, HashSet, LinkedHashSet, for loop, while loop, if statement, char[] array, String functions.

About Author:

I am simple guy with lot of ambitions. My main motive is to share whatever knowledge I have related to programming. With me you can easily learn how to solve any programming problem in Java.You can connect with me on social networking sites also.

Let's Get Connected: Linkedin | Facebook |

How to find Duplicate characters in given String in Java? How to find Duplicate characters in given String in Java? Reviewed by Rohit Agarwal on 10/23/2016 Rating: 5


  1. Hello Rohit,

    Can't we solve this problem in little simpler and short way.
    Because we have to just count the number of characters that are repeated.
    Can't we do it just using HashMap().
    If we then the solution will become very small and easy to understand.
    May be I am wrong. Please update me on this

    1. Hi Piyush,

      We have to print duplicate characters in String. Yes, we can do this problem using HashMap() but I don't think so that is short and simple because we have to perform following steps.

      1) Break string into characters.
      2) Found the count corresponding to each character -> To achieve this we need nested loop.
      3) Take character as a Key and character count as a value, then store it into HashMap().
      4) In HashMap() check for which Key , Value is greater than 1 and finally print Keys.

      If you have any better approach to solve this problem using HashMap() please share with us. I will be more happy to see your approach :)

    2. Hello Rohit

      I think we can solve this problem using following steps,
      HashMap map,
      for i=0, i<length(string), i++

    3. I don't know the exact function name but I think we can do it in this manner.
      Please response..
      Thank you.

  2. Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article.
    software testing course in Chennai

  3. Rohit bhai appke hashset article mere interview clear me kaam aaya. Sir ji aapka bahut thanks.


  4. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
    Digital Marketing Training


Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.

Powered by Blogger.