Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.Set;
- public class Main
- {
- public static void main(String args[])
- {
- //print duplicate characters
- print("abaabcdehde");
- }
- //Find all duplicate characters in a String and print each of them.
- public static void print(String word)
- {
- char[] characters = word.toCharArray();
- // build HashMap with character and number of times they appear in String
- Map<Character, Integer> charMap = new HashMap<Character, Integer>();
- for (Character ch : characters) {
- if (charMap.containsKey(ch)) {
- charMap.put(ch, charMap.get(ch) + 1);
- } else {
- charMap.put(ch, 1);
- }
- }
- // Iterate through HashMap to print all duplicate characters of String
- Set<Map.Entry<Character, Integer>> entrySet = charMap.entrySet();
- System.out.println("List of duplicate characters "+word);
- for (Map.Entry<Character, Integer> entry : entrySet) {
- if (entry.getValue() > 1) {
- System.out.println(entry.getKey()+" - "+entry.getValue());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement