-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetIntersection.java
More file actions
59 lines (46 loc) · 1.18 KB
/
Copy pathSetIntersection.java
File metadata and controls
59 lines (46 loc) · 1.18 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
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
/**
*You are given two sorted list of numbers(ascending order). The lists themselves are comma delimited
*and the two lists are semicolon delimited. Print out the intersection of these two sets.
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class intersection {
public static void main(String[] args) {
String file = args[0];
read(file);
}
public static void read(String f) {
try {
BufferedReader in = new BufferedReader(new FileReader(f));
String str;
while ((str = in.readLine()) != null) {
System.out.println(process(str));
}
in.close();
} catch (IOException e) {
}
}
private static String process(String s) {
String[] stuff = s.split(";");
String[] firstSet = stuff[0].split(",");
String[] secondSet = stuff[1].split(",");
return compair(firstSet, secondSet);
}
private static String compair(String[] first, String[] second) {
int i, j, k = 0;
String s = "";
for (i = 0; i < first.length; i++) {
for (j = 0; j < second.length; j++) {
if (first[i].equalsIgnoreCase(second[j])) {
k++;
if (k >= 2) {
s += ",";
}
s += first[i];
}
}
}
return s;
}
}