349. intersection of two arrays

Given two arrays, write a function to compute their intersection.

Example 1:

1
2
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]

Example 2:

1
2
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]
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
class  {
public int[] intersection(int[] nums1, int[] nums2) {
List<Integer> list = new ArrayList<>();
int[] nums = null;
if (nums1 == null || nums2 == null || nums1.length == 0 || nums2.length == 0) {
nums = new int[0];
return nums;
}
for (int i = 0; i < nums1.length; i++) {
for (int j = 0; j < nums2.length; j++) {
if (nums1[i] == nums2[j]) {
if (!list.contains(nums1[i])) {
list.add(nums1[i]);
}
}
}
}
int size = list.size();
nums = new int[size];
for (int i = 0; i < size; i++) {
nums[i] = list.get(i);
}
return nums;
}
}