-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
43 lines (40 loc) · 1018 Bytes
/
Solution.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
package Array.No_349_Intersection_of_Two_Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* FileName: Solution
* Author: EdisonLi的Windows
* Date: 2019/6/11 16:52
* Description:
* Given two arrays, write a function to compute their intersection.
*
* Example 1:
*
* Input: nums1 = [1,2,2,1], nums2 = [2,2]
* Output: [2]
* Example 2:
*
* Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
* Output: [9,4]
* Difficulty: Easy
*/
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<>();
Set<Integer> set2 = new HashSet<>();
for (int i = 0; i < nums1.length; i++){
set1.add(nums1[i]);
}
for (int j = 0; j < nums2.length; j++){
if (set1.contains(nums2[j])){
set2.add(nums2[j]);
}
}
int[] res = new int[set2.size()];
int o = 0;
for (int i:set2){
res[o++] = i;
}
return res;
}
}