Java Program to find repeated Characters in a String


Problem

write a java program to find repeated characters in a String.
For example:
Input: “code2master”
Output: “e ==> 2”
As char ‘e’ is repeated twice in “code2master”.


Solution

Java program is very straightforward.

  • Create a Map of type Character and Integer. Characters in String will be stored as key and count as the value.
  • If character is not present in hashmap, just add in hashmap with count as 1.
  • If character is present in hashmap, add count by 1.
  • At last, iterate over hashmap’s key and print all the characters whose count is greater than 1.

Java program to find repeated character

e ==> 2

That’s all about find repeated character in the String in java.

Leave a Reply

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