forked from surajr/CodingInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersection.java
More file actions
42 lines (26 loc) · 723 Bytes
/
intersection.java
File metadata and controls
42 lines (26 loc) · 723 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
/*
Using HashSet
Time complexity O(n)
Space Complexity O(n)
*/
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> set = new HashSet<>();
HashSet<Integer> result = new HashSet<>();
for(int num: nums1)
set.add(num);
for(int num: nums2)
if(set.contains(num))
result.add(num);
int [] resultArray = new int[result.size()];
int i = 0;
for(int num: result)
resultArray[i++] = num;
return resultArray;
}
}
/*
Using Sorting and Two pointers
Time complexity O(nlogn)
Space complexity O(1)
*/