548.Intersection of Two Arrays II

1.Description(Easy)

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

Notice

  • Each element in the result should appear as many times as it shows in both arrays.

  • The result can be in any order.

Example

Givennums1=[1, 2, 2, 1],nums2=[2, 2], return[2, 2].

Challenge

  • What if the given array is already sorted? How would you optimize your algorithm?

  • What if nums1's size is small compared to num2's size? Which algorithm is better?

  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

2.Code

Two pointers

  public int[] intersection(int[] nums1, int[] nums2) {
     if(nums1==null || nums2==null){
            return null;
        }
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int index1=0;
        int index2=0;
        ArrayList<Integer> list=new ArrayList<Integer>();
        while(index1<nums1.length && index2<nums2.length){
            if(nums1[index1]>nums2[index2]){
                index2++;
            }else if(nums1[index1]<nums2[index2]){
                index1++;
            }else{
                list.add(nums1[index1]);
                index1++;
                index2++;
            }

        }
        int[] result=new int[list.size()];
        for(int i=0;i<list.size();i++){
            result[i]=list.get(i);
        }
        return result;
    }

Last updated