Aryan PrajapatKnowledge Contributor
Write a program that detects the duplicate characters in a string.
Write a program that detects the duplicate characters in a string.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Questions | Answers | Discussions | Knowledge sharing | Communities & more.
package simplilearnJava;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class FindDuplicate {
public static void main(String args[]) {
printDuplicateCharacters(“Simplilearn”);
}
public static void printDuplicateCharacters(String word) {
char[] characters = word.toCharArray();
Map charMap = new HashMap();
for (Character ch : characters) {
if (charMap.containsKey(ch)) {
charMap.put(ch, charMap.get(ch) + 1);
} else {
charMap.put(ch, 1);
}
}
Set<Map.Entry> entrySet = charMap.entrySet();
System.out.printf(“List of duplicate characters in String ‘%s’ %n”, word);
for (Map.Entry entry : entrySet) {
if (entry.getValue() > 1) {
System.out.printf(“%s: %d %n”, entry.getKey(), entry.getValue());
}
}
}
}