-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapProblem.java
35 lines (31 loc) · 899 Bytes
/
MapProblem.java
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
29
30
31
32
33
34
35
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class MapProblem {
public static void main(String[] args) {
String s = "Apple apple dog blue Blue blue Cat";
String[] words = s.split(" ");
System.out.println(Arrays.toString(words));
// create a count for every word in s, ignoring case
// returns the string that occurs the most
String result = modeString(words);
System.out.println(result);
}
public static String modeString(String[] words) {
Map<String, Integer> counts = new HashMap<>();
String mode = null;
for (String word : words) {
word = word.toLowerCase();
if (!counts.containsKey(word)) {
counts.put(word, 0);
}
// increase the count of word by 1
int count = counts.get(word) + 1;
counts.put(word, count);
if (mode == null || count > counts.get(mode)) {
mode = word;
}
}
return mode;
}
}