-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrequencyCounter.java
More file actions
29 lines (26 loc) · 1.04 KB
/
Copy pathFrequencyCounter.java
File metadata and controls
29 lines (26 loc) · 1.04 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
/*
* Sample usage: java FrequencyCounter 1 < tinyTale.txt
* Sample usage: java FrequencyCounter 8 < tale.txt
*/
public class FrequencyCounter {
public static void main(String[] args){
int minlen = Integer.parseInt(args[0]); // key-length cutoff
// st could be: SequentialSearchST, BinarySearchST, BST,
// RedBlackBST, SeparateChainingST, LinearProbingHashST
LinearProbingHashST<String, Integer> st = new LinearProbingHashST<>();
while(!StdIn.isEmpty()){
// Build symbol table and count frequencies.
String word = StdIn.readString();
if (word.length() < minlen) continue; // Ignore short keys.
if (!st.contains(word)) st.put(word, 1);
else st.put(word, st.get(word)+1);
}
// Find a key with the highest frequency count.
String max = "";
st.put(max, 0);
for (String word: st.keys())
if (st.get(word)>st.get(max))
max = word;
StdOut.println(max + " " + st.get(max));
}
}