349. Intersection of Two Arrays

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]
Note:
  • Each element in the result must be unique.
  • The result can be in any order.
---
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
int i = 0, j = 0;
List<Integer> list = new ArrayList<Integer>();
while (i < nums1.length && j < nums2.length) {
if (nums1[i] == nums2[j]) {
list.add(nums1[i]);
i++;
j++;
while (i < nums1.length && nums1[i] == nums1[i - 1]) {
i++;
}
while (j < nums2.length && nums2[j] == nums2[j - 1]) {
j++;
}
} else if (nums1[i] < nums2[j]) {
i++;
} else {
j++;
}
}
int[] ans = new int[list.size()];
for (i = 0; i < list.size(); i++) {
ans[i] = list.get(i);
}
return ans;
}
}