To print the duplicate characters from a string in Java, you can use a Map
to count the frequency of each character in the string and then iterate over the map to find and print the characters that occur more than once.
Here's an example of how you could do this:
String str = "abcabcdefg"; // Create a map to count the frequency of each character Map<Character, Integer> charCount = new HashMap<>(); for (char c : str.toCharArray()) { charCount.put(c, charCount.getOrDefault(c, 0) + 1); } // Iterate over the map and print the characters that occur more than once for (Map.Entry<Character, Integer> entry : charCount.entrySet()) { if (entry.getValue() > 1) { System.out.println(entry.getKey()); } }Sruoce:www.lautturi.com
This code will create a map that counts the frequency of each character in the string, and then iterate over the map and print the characters that have a count greater than 1.
The output of this code will be:
a b c
Keep in mind that this code assumes that the input string contains only ASCII characters. If you want to handle Unicode characters or other types of input, you may need to modify the code accordingly.