How to count Duplicate character in java using HashMap

In this blog we are going to learn how to count duplicate character in java using HashMap in java


Code :

package learningJava;
import java.util.*;
public class Learnig {
public static void main(String[] args) {
HashMap<Character,Integer> map = new HashMap<>();
String s = "##$$aabbcCdeaaF*  ,,,()+-****";
char[] charArray = s.toCharArray();
for(int i =0;i<charArray.length;i++) {
if(!map.containsKey(charArray[i])) {
map.put(charArray[i], 1);
}
else {
map.put(charArray[i], map.get(charArray[i])+1);
}
}
System.out.print(map);
}
}


Output

{ =2, a=4, b=2, #=2, c=1, C=1, $=2, d=1, e=1, F=1, (=1, )=1, *=5, +=1, ,=3, -=1 }




 

Post a Comment

0 Comments