-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hashmap10.java
45 lines (39 loc) · 1.13 KB
/
Hashmap10.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
36
37
38
39
40
41
42
43
44
45
import java.util.*;
public class Hashmap10
{
public static void main(String[] Google)
{
Scanner sobj = new Scanner(System.in);
System.out.println("Enter String : ");
String str = sobj.nextLine();
str = str.trim();
str = str.replaceAll("\\s+", " ");
int Frequency = 0;
String Arr[] = str.split(" ");
HashMap<String, Integer> hobj = new HashMap<String, Integer>();
for(String a : Arr)
{
if(hobj.containsKey(a)) // String is already present
{
Frequency = hobj.get(a);
hobj.put(a,Frequency+1);
}
else // String is occured for the first time
{
hobj.put(a, 1);
}
}
Set <String>setobj = hobj.keySet();
int iMax = 0;
String temp = null;
for(String B : setobj)
{
if(hobj.get(B) > iMax)
{
iMax = hobj.get(B);
temp = B;
}
}
System.out.println(temp + " occured maximum ie " + iMax + " times");
}
}