-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMosrFrequentIPAddresses.java
65 lines (54 loc) · 2.09 KB
/
MosrFrequentIPAddresses.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MyGroupRegex {
private static Pattern ptn = Pattern.compile("((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))");
//Pattern.compile("(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})");
public static List<String> captureValues(String[] arr){
String largeText = Arrays.toString(arr);
System.out.println(largeText);
Matcher mtch = ptn.matcher(largeText);
List<String> ips = new ArrayList<String>();
while(mtch.find()){
ips.add(mtch.group());
}
HashMap<String,Integer> map = new HashMap<>();
for(String eachIp: ips)
{
if(map.containsKey(eachIp))
map.put(eachIp,map.get(eachIp)+1);
else
map.put(eachIp,1);
}
int max= Integer.MIN_VALUE;
for(Entry<String,Integer> entry:map.entrySet())
{
if(entry.getValue()>=max)
{
max=entry.getValue();
}
}
//have highest count here
// System.out.println(max);
List<String> answer = new ArrayList<>();
for(Entry<String,Integer> entry:map.entrySet())
{
if(entry.getValue()==max)
{
answer.add(entry.getKey());
}
}
return answer;
}
public static void main(String a[]){
String [] str = {"Hi my machine IP is 10.20.30.40 and i would like to access port 80 from the host 23.12.56.34, which internally connects to 3.90.23.65. Please process the request",
"Hi my machine IP is 10.20.30.40 and i would like to access port 80 from the host 23.12.56.34",
"host 23.12.56.34",
"host 23.12.56.34 is ",
"host is useless at 23.12.56.34",
"10.20.30.40 is actually not but 10.20.30.50. Please 10.20.30.40fix10.20.30.40"
};
System.out.println(captureValues(str));
}
}