-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmostFrequentWord.java
More file actions
47 lines (39 loc) · 1.35 KB
/
mostFrequentWord.java
File metadata and controls
47 lines (39 loc) · 1.35 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.List;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
class Solution{
public static List<String> mostFrequentWord(String par, List<String> ignoreWords){
String[] words = par.split("\\s");
HashMap<String, Integer> map = new HashMap<>();
for (String word: words){
if (!ignoreWords.contains(word)){
if (!map.containsKey(word)){
map.put(word, 1);
} else {
int old = map.get(word);
map.replace(word, old, old + 1);
}
}
}
int highestOccurence = 0;
for (Map.Entry<String, Integer> entry: map.entrySet()){
if (entry.getValue() > highestOccurence){
highestOccurence = entry.getValue();
}
}
List<String> result = new ArrayList<String>();
for (Map.Entry<String, Integer> entry: map.entrySet()){
if (entry.getValue() == highestOccurence){
result.add(entry.getKey());
}
}
return result;
}
public static void main(String[] str){
List<String> exclude = new ArrayList<String>();
exclude.add("hi");
System.out.println(mostFrequentWord("hi my name is austin austin", exclude));
}
}