Step1:- Creating a HashMap containing char as key and it's occurrences as value.
Step2:- Converting given string to char array.
Step3:- checking each char of strArray.
Step4:- If char is present in charCountMap, incrementing it's count by 1.
Step5:- If char is not present in charCountMap,
putting this char to charCountMap with 1 as it's value.
Step6:- Getting a Set containing all keys of charCountMap.
Step7:- Iterating through Set 'charsInString‘
Step8:- If any char has a count of more than 1, printing it's count
=========================================================================
package com.interviewprogramcomm;
import java.util.HashMap;
import java.util.Set;
public class FindCharCountInStr {
public static void findcharcountInStr(String str){
HashMap<Character,Integer> hm=new HashMap<Character,Integer>();
char[] c=str.toCharArray();
for(char c1:c){
if(hm.containsKey(c1)){
hm.put(c1,hm.get(c1)+1);
}
else{
hm.put(c1,1);
}
}
Set<Character> set= hm.keySet();
for(Character c2:set){
if(hm.get(c2)>1){
System.out.println(c2+"::"+ hm.get(c2));
}
}
}
public static void main(String[] args) {
findcharcountInStr("javajavaM");
}
}