Skip to content

Commit f2dfbaf

Browse files
committed
most frequent k words done using min-heap and hashmap
1 parent 96886ad commit f2dfbaf

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/heap/KMostFrequent.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
public class KMostFrequent {
99

10-
static Data[] datas = new Data[5];
10+
static Data[] datas = new Data[4];
1111
static int size;
1212

1313
private static void addToHeap(Data data) {
@@ -48,10 +48,10 @@ private static void heapify(int k) {
4848

4949
if (min != k) {
5050
Data data = datas[min];
51-
datas[k] = data;
52-
datas[k].heapIndex = k;
5351
datas[min] = datas[k];
5452
datas[min].heapIndex = min;
53+
datas[k] = data;
54+
datas[k].heapIndex = k;
5555
heapify(min);
5656
}
5757
}
@@ -80,6 +80,15 @@ public Data(String word, int count, int heapIndex) {
8080
this.word = word;
8181
this.heapIndex = -1;
8282
}
83+
84+
@Override
85+
public String toString() {
86+
return "Data{" +
87+
"count=" + count +
88+
", word='" + word + '\'' +
89+
", heapIndex=" + heapIndex +
90+
'}';
91+
}
8392
}
8493

8594
private static HashMap<String, Data> wordCountMap = new HashMap<String, Data>();
@@ -101,12 +110,7 @@ private static void addWord(String word) {
101110
}
102111

103112
public static void main(String[] strings) {
104-
String s = "Welcome to the world of Geeks " +
105-
"This portal has been created to provide well written well thought and well explained " +
106-
"solutions for selected questions If you like Geeks for Geeks and would like to contribute " +
107-
"here is your chance You can write article and mail your article to contribute at " +
108-
"geeksforgeeks org See your article appearing on the Geeks for Geeks main page and help" +
109-
"thousands of other Geeks";
113+
String s = "A B C D A B A E E E E F F F";
110114
String[] strings1 = s.split(" ");
111115
for (String s1 : strings1) {
112116
addWord(s1);

0 commit comments

Comments
 (0)