- public class CountOfCharacter {
- public static void main(String[] args) {
- String s = "abcabcabc";
- countOfRepeatedCharacter(s);
- }
- public static void countOfRepeatedCharacter(String s) {
- char c[] = s.toCharArray();
- Map<Character, Integer> map = new HashMap<Character, Integer>();
- for (int i = 0; i < c.length; i++) {
- if (map.containsKey(c[i])) {
- map.put(c[i], map.get(c[i]) + 1);
- } else {
- map.put(c[i], 1);
- }
- }
- System.out.println(map);
- }
- }
Output:
{a=3, b=3, c=3}
No comments:
Post a Comment