display only repeated letters in a string in Java:
In this tutorial, you will learn how to display only repeated letters in a string in the Java program.
import java.util.*;
public class printallduplicatecharc {
public static void
countDuplicateCharacters(String str)
{
Map map
= new HashMap();
char[] charArray = str.toCharArray();
for (char c : charArray) {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
}
else {
map.put(c, 1);
}
}
for (Map.Entry entry :
map.entrySet()) {
if (entry.getValue() > 1) {
System.out.println(entry.getKey()
+ " : "
+ entry.getValue());
}
}
}
// Driver Code
public static void main(String args[])
{
System.out.println("Enter A Santense For Find Repeated Character ");
Scanner sc= new Scanner(System.in);
String str = sc.nextLine();
System.out.println("Enter By You \n" + str);
// Function Call
System.out.println("Repated Value ");
countDuplicateCharacters(str);
}
}