Java program to print all duplicate characters in a string Last Updated : 15 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given a string, the task is to write Java program to print all the duplicate characters with their frequency Example: Input: str = "geeksforgeeks" Output: s : 2 e : 4 g : 2 k : 2 Input: str = "java" Output: a : 2 Approach: The idea is to do hashing using HashMap. Create a hashMap of type {char, int}.Traverse the string, check if the hashMap already contains the traversed character or not.If it is present, then increment the count or else insert the character in the hashmap with frequency = 1.Now traverse through the hashmap and look for the characters with frequency more than 1. Print these characters with their respective frequencies. Below is the implementation of the above approach: Java // Java program for the above approach import java.util.*; class GFG { // Function to print all duplicate // characters in string using HashMap public static void countDuplicateCharacters(String str) { // Creating a HashMap containing char // as a key and occurrences as a value Map<Character, Integer> map = new HashMap<Character, Integer>(); // Converting given string into // a char array char[] charArray = str.toCharArray(); // Checking each character // of charArray for (char c : charArray) { if (map.containsKey(c)) { // If character is present // in map incrementing it's // count by 1 map.put(c, map.get(c) + 1); } else { // If character is not present // in map putting this // character into map with // 1 as it's value. map.put(c, 1); } } // Traverse the HashMap, check // if the count of the character // is greater than 1 then print // the character and its frequency for (Map.Entry<Character, Integer> entry : map.entrySet()) { if (entry.getValue() > 1) { System.out.println(entry.getKey() + " : " + entry.getValue()); } } } // Driver Code public static void main(String args[]) { // Given String str String str = "geeksforgeeks"; // Function Call countDuplicateCharacters(str); } } Output:s : 2 e : 4 g : 2 k : 2 Time Complexity: O(NlogN) Auxiliary Space: O(N) since using Map Comment More info P prashant_srivastava Follow Improve Article Tags : Strings Hash Java Programs DSA Hash Java-HashMap +2 More Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 14 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 6 min read Problem of The Day - Develop the Habit of Coding 5 min read Like