Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int[] intersection(int[] nums1, int[] nums2) {
- Arrays.sort(nums1);
- Arrays.sort(nums2);
- List<Integer> res = new ArrayList<>();
- int i = 0;
- int j = 0;
- while (i < nums1.length && j < nums2.length) {
- if (nums1[i] < nums2[j]) i++;
- else if (nums1[i] > nums2[j]) j++;
- else {
- res.add(nums1[i]);
- do {
- i++;
- } while (i < nums1.length && nums1[i] == nums1[i - 1]);
- do{
- j++;
- } while (j < nums2.length && nums2[j] == nums2[j - 1]);
- }
- }
- int[] result = new int[res.size()];
- for (int k = 0; k < result.length; k++) result[k] = res.get(k);
- return result;
- }
- }
Add Comment
Please, Sign In to add comment