import java.util.HashMap; public class CountOccurance { // HashMap KEY: character STRING :count/no of Occurrence public static HashMap<Character, Integer> charOccurrence = new HashMap<Character, Integer>(); static void count(String string) { // Character array char[] _array = string.toCharArray(); for (char character : _array) { if (charOccurrence.containsKey(character)) { int _count=charOccurrence.get(character)+1; charOccurrence.put(character, _count); } else { charOccurrence.put(character, 1); } } } public static void main(String[] args) { String _str = "AASBDFGDDDDFFHFSSB"; count(_str); System.out.println(charOccurrence); } } Output:{D=5, F=4, G=1, A=2, B=2, S=3, H=1}
-
Blogger Comment
-
Facebook Comment
Subscribe to:
Post Comments
(
Atom
)
0 comments :
Post a Comment