forked from joharbatta/DataStructure-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmostFreqword.java
More file actions
39 lines (37 loc) · 1004 Bytes
/
mostFreqword.java
File metadata and controls
39 lines (37 loc) · 1004 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
29
30
31
32
33
34
35
36
37
38
39
import java.util.*;
public class mostfreqword{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
String str = s.nextLine();
freqWords(str);
}
static void freqWords(String s)
{
String[] words=s.split("\\s");
HashMap<String, Integer> hs = new HashMap<String, Integer>();
for (int i = 0; i < words.length; i++)
{
if(hs.containsKey(words[i]))
{
hs.put(words[i], hs.get(words[i]) + 1);
}
else
{
hs.put(words[i], 1);
}
}
int max=0;
String key = "";
for (String x: hs.keySet())
{
if(max<hs.get(x))
{
max=hs.get(x);
key=x;
}
// System.out.println("key: " + x + " value: " + hs.get(x)+" "+max);
}
System.out.println("Most Frequent Word is "+key);
}
}