Java Program to Find Duplicate Characters in a String With Repetition Count

Write a Java program to find duplicate characters in a String with the repetition count is asked in many interviews. This post gives two ways to write a program for this problem.

1- You can use a HashMap to find duplicate characters in a String along with repetition count.
2- If you are asked not to use any inbuilt structure or API then you can write logic to find duplicate characters in a String using for loops.

Find duplicate characters in a String Java program using HashMap

In HashMap you can store each character in such a way that the character becomes the key and the count is value. For each character check in HashMap if char already exists; if yes then increment count for the existing char, if no then add the char to the HashMap with the initial count as 1.

Output

Find duplicate characters in a String Java program using loops

If you need to write logic to find duplicates by yourself then you can use outer and inner for loops to do that. In the outer loop iterate the String one character at a time and in the inner loop scan the String for the same character. If found increment count for it.

That’s all for the topic Java Program to Find Duplicate Characters in a String With Repetition Count. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.