Intersection of Two Arrays II

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

Example 1:

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

Example 2:

Input: 
nums1 = 
[4,9,5]
, nums2 = 
[9,4,9,8,4]
Output: 
[4,9]

Note:

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

  • The result can be in any order.

Follow up:

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

  • What if nums1 's size is small compared to nums2 '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?

Analysis

这里讨论follow up的问题:

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

If both arrays are sorted, we could use two pointers to iterate, which is similar to the merge two sorted array process.

Use two pointers. At start two pointers point to the first element of each array. If it is equal, push it to the answer then move both pointers to next position. Otherwise, move the one with less value to the next position.

Using two pointers will have O(M + N) time complexity

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

If using HashMap solution, the time complexity would be O(M + N), while space complexity is O(N), we can use nums1 to build the hash map, in order to reduce space complexity.

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?

If nums1 is small enough to store in memory, build a hashmap out of it, and read chunk by chunk from nums2, and check if it's in hashmap.

If both nums1 and nums2 are too large for in memory storage, do external sorting for both first, and read chunk by chunk for both sorted array, and then check intersection.

Solution

HashMap Approach - O(n) time, O(n) space

public int[] intersect(int[] nums1, int[] nums2) {

    Map<Integer, Integer> map = new HashMap<Integer, Integer>();

    int maxSize = Math.min(nums1.length, nums2.length);
    int[] result = new int[maxSize];
    int size = 0;

    for (int i : nums1) {
        map.put(i, map.getOrDefault(i, 0) + 1);
    }

    for (int i : nums2) {
        if (map.getOrDefault(i, 0) > 0) {
            result[size++] = i;
            map.put(i, map.get(i) - 1);
        }
    }

    return Arrays.copyOf(result, size);

}

Sort two Arrays, then use two pointers

public class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        List<Integer> res = new ArrayList<Integer>();
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        for(int i = 0, j = 0; i< nums1.length && j<nums2.length;){
                if(nums1[i]<nums2[j]){
                    i++;
                }
                else if(nums1[i] == nums2[j]){
                    res.add(nums1[i]);
                    i++;
                    j++;
                }
                else{
                    j++;
                }
        }
        int[] result = new int[res.size()];
        for(int i = 0; i<res.size();i++){
            result[i] = res.get(i);
        }
        return result;
    }
}

Last updated