- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountCharacters.java
More file actions
Latest commit
28 lines (18 loc) · 511 Bytes
/
Copy pathCountCharacters.java
File metadata and controls
28 lines (18 loc) · 511 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
packagecount;
importjava.util.HashMap;
importjava.util.Map;
publicclassCountCharacters {
publicstaticvoidmain(String[] args) {
Stringname = "Find the character count of this sentence.!";
char[] split = name.toCharArray();
Map<Character, Integer> count = newHashMap<Character, Integer>();
for (charletter : split) {
if (count.containsKey(letter)) {
count.replace(letter, count.get(letter) + 1);
} else {
count.put(letter, 1);
}
}
System.out.println(count);
}
}