-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersection.java
More file actions
73 lines (58 loc) · 1.49 KB
/
Copy pathintersection.java
File metadata and controls
73 lines (58 loc) · 1.49 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.*;
/*
* Given two arrays, write a function to compute their intersection.
* Example:
* Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
* Note:
Each element in the result must be unique.
The result can be in any order.
*/
public class intersection {
public static int[] intersection(int[] nums1, int[] nums2) {
if (nums1 == null || nums2 == null) return null;
Set<Integer> array = new HashSet<Integer>();
Set<Integer> common = new HashSet<Integer>();
for (int i = 0; i < nums1.length; i++) {
array.add(nums1[i]);
}
for (int j = 0; j < nums2.length; j++) {
if (array.contains(nums2[j])) {
common.add(nums2[j]);
}
}
int[] result = new int[common.size()];
int k = 0;
for (int integer : common) {
result[k] = integer;
k += 1;
}
return result;
}
public static int[] findCommon (int[] s, int[] l) {
Set<Integer> sArray = new HashSet<Integer>();
Set<Integer> common = new HashSet<Integer>();
for (int i = 0; i < s.length; i++) {
sArray.add(s[i]);
}
for (int j = 0; j < l.length; j++) {
if (sArray.contains(l[j])) {
common.add(l[j]);
}
}
int[] result = new int[common.size()];
int k = 0;
for (int integer : common) {
result[k] = integer;
k += 1;
}
return result;
}
public static void main(String[] args) {
int[] a = {1, 2, 2, 15, 3};
int[] b = {2, 5, 7, 15, 1};
int[] res = intersection(a, b);
for (int i = 0; i < res.length; i++) {
System.out.print(" " + res[i]);
}
}
}