Write a Java program to find duplicate characters in a String with the repetition count is asked in many interviews. This post gives two ways to write a program for this problem.
- You can use a HashMap to find duplicate characters in a String along with repetition count.
- If you are asked not to use any inbuilt structure or API then you can write logic to find duplicate characters in a String using for loops.
Find duplicate characters in a String Java program using HashMap
In HashMap you can store each character in such a way that the character becomes the key and the count is value. For each character check in HashMap if char already exists; if yes then increment count for the existing char, if no then add the char to the HashMap with the initial count as 1.
public class DuplicateChars { public static void main(String[] args) { findDuplicateChars("How many duplicates in this string"); findDuplicateChars("Addis Ababa"); } private static void findDuplicateChars(String str) { System.out.println("Duplicates in- "+ str); char[] strArr = str.toCharArray(); Map<Character, Integer> charMap = new HashMap<>(); for(char c : strArr) { // Ignore spaces if(c == ' ') continue; // check if character already exists if(charMap.containsKey(c)) { charMap.put(c, charMap.get(c) + 1); }else { charMap.put(c, 1); } } // Iterating collection view of the Map Set<Map.Entry<Character, Integer>> countSet = charMap.entrySet(); for(Map.Entry<Character, Integer> entry : countSet){ if(entry.getValue() > 1) { System.out.println(entry.getKey() + " found " + entry.getValue() + " times"); } } } }Output
Duplicates in- How many duplicates in this string a found 2 times i found 4 times n found 3 times s found 3 times t found 3 times Duplicates in- Addis Ababa A found 2 times a found 2 times b found 2 times d found 2 times
Find duplicate characters in a String Java program using loops
If you need to write logic to find duplicates by yourself then you can use outer and inner for loops to do that. In the outer loop iterate the String one character at a time and in the inner loop scan the String for the same character. If found increment count for it.
public class DuplicateChars { public static void main(String[] args) { findDuplicateChars("How many duplicates in this string"); findDuplicateChars("Addis Ababa"); } private static void findDuplicateChars(String str) { System.out.println("Duplicates in- "+ str); int count; for(int i = 0; i < str.length(); i++) { // get a character char c = str.charAt(i); //starting count for any character count = 1; //ignore spaces if(c == ' ') continue; for(int j = i + 1; j < str.length(); j++) { if(c == str.charAt(j)) { count++; // remove the char which is already counted str = str.substring(0, j) + str.substring(j+ 1); } } if(count > 1) { System.out.println(c + " found " + count + " times"); } } } }
That's all for the topic Java Program to Find Duplicate Characters in a String With Repetition Count. If something is missing or you have something to share about the topic please write a comment.
You may also like
- How to Convert String to Byte Array in Java
- How to Convert String to double in Java
- Convert HTML to PDF in Java Using Openhtmltopdf, PDFBox
- Display Time in 24 Hour Format in Java
- throw Vs throws in Java Exception Handling
- Java LinkedHashSet With Examples
- First React App – Hello World React Example
- How to Create Thread in Python
No comments:
Post a Comment