Java program to print all duplicate characters in a string Last Updated : 07 Aug, 2022 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 infoAdvertise with us Next Article Java program to print all duplicate characters in a string prashant_srivastava Follow Improve Article Tags : Strings Hash Java Programs DSA Hash Java-HashMap +2 More Practice Tags : HashHashStrings Similar Reads Java Program to Iterate Over Characters in String Given string str of length N, the task is to traverse the string and print all the characters of the given string using java. Illustration: Input : str = âGeeksforGeeksâ Output : G e e k s f o r G e e k sInput : str = "GfG" Output : G f G Methods: Using for loops(Naive approach)Using iterators (Opti 3 min read Iterate Over the Characters of a String in Java Given string str of length N, the task is to traverse the string and print all the characters of the given string. Illustration: Input : âGeeksforGeeksâ Output : G e e k s f o r G e e k sInput. : âCoderâ Output : C o d e r Methods: Using Naive ApproachUsing String.toCharArray() methodUsing Character 9 min read Java program to count the occurrence of each character in a string using Hashmap Given a string, the task is to write a program in Java which prints the number of occurrences of each character in a string. Examples: Input: str = "GeeksForGeeks" Output: r 1 s 2 e 4 F 1 G 2 k 2 o 1 Input: str = "Ajit" Output: A 1 t 1 i 1 j 1 An approach using frequency[] array has already been dis 2 min read Java Program for Last duplicate element in a sorted array We have a sorted array with duplicate elements and we have to find the index of last duplicate element and print index of it and also print the duplicate element. If no such element found print a message. Examples: Input : arr[] = {1, 5, 5, 6, 6, 7} Output : Last index: 4 Last duplicate item: 6 Inpu 2 min read Java Program to Print all Unique Words of a String Java program to print all unique words present in the string. The task is to print all words occurring only once in the string. Illustration: Input : Welcome to Geeks for Geeks. Output : Welcome to for Input : Java is great.Python is also great. Output : Java Python also Methods: This can be done in 4 min read How to Remove Duplicates from a String in Java? Working with strings is a typical activity in Java programming, and sometimes we need to remove duplicate characters from a string. In this article we have given a string, the task is to remove duplicates from it. Example Input: s = "geeks for geeks"Output: str = "geks for" Remove Duplicates From a 2 min read Java Program to Count the Occurrences of Each Character In Java, counting the occurrences of each character in a string is a fundamental operation that can be done in different ways. This process involves identifying the frequency of each character in the input string and displaying it in a readable format.Example: Input/output to count the occurrences o 5 min read How to Remove Duplicates from a String in Java Using Regex ? In Java programming, Regex (regular expressions) is a very important mechanism. It can be used to match patterns in strings. Regular expressions provide a short and simple syntax for describing the patterns of the text. In this article, we will be learning how to remove duplicates from a String in J 2 min read Java Program To Recursively Remove All Adjacent Duplicates Given a string, recursively remove adjacent duplicate characters from the string. The output string should not have any adjacent duplicates. See the following examples. Examples: Input: azxxzy Output: ay First "azxxzy" is reduced to "azzy". The string "azzy" contains duplicates, so it is further red 5 min read Count Occurrences of a Given Character using Regex in Java Given a string and a character, the task is to make a function that counts the occurrence of the given character in the string using Regex. Examples: Input: str = "geeksforgeeks", c = 'e' Output: 4 'e' appears four times in str. Input: str = "abccdefgaa", c = 'a' Output: 3 'a' appears three times in 2 min read Like