Program to find duplicate character in java

Let’s seeĀ Program to find duplicate character in java.

package programnew;

import java.util.*;

public class DisplayDuplicateCharecter {
	public static void displayCharecter(String str) {
		Map<Character, Integer> mapObj = new HashMap<>();
		char[] chrs = str.toCharArray();
		for (char ch : chrs) {
			if (!mapObj.containsKey(ch)) {
				mapObj.put(ch, 1);
			} else {
				mapObj.put(ch, mapObj.get(ch) + 1);
			}
		}
		 
		for (Map.Entry<Character, Integer> entry : mapObj.entrySet()) {
			if (entry.getValue() > 1) {
				System.out.println(entry.getKey() + " " + entry.getValue());
			}
		}
	}

	public static void main(String[] args) {
		String str = "anjalikumari";
		displayCharecter(str);
	}
}

 

Output is –


a 3
i 2