leetcode 349 Intersection Of Two Arrays

Posted by bebelushu on Wed, 31 Jul 2019 14:34:15 +0200

See the title details. Click here.

subject

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.

Thoughts on Problem Solving

In order to get the intersection of two arrays, we can use nested loops to compare elements one by one and use Set to remove duplicate elements, but this method is inefficient. However, we can consider sorting the arrays first. Because the elements are ordered, the arrays with smaller lengths can be put into the queue, and the arrays with longer loops can be compared with the first part of the queue to make the queue operation. Set is also used to remove the overlapping intersection elements, which improves the execution efficiency a lot. .

However, it is more efficient to iterate through arrays, using Map to store elements with key value, traversing the first array with 1 value, traversing the second array with 2 key value. After two cycles, the key value of value 2 in Map is the intersection result required by the title.

code implementation

private static int[] convertSetToArray(Set<Integer> intersectionSet) {
    Integer[] intersection = new Integer[intersectionSet.size()];
    intersectionSet.toArray(intersection);
    int[] result = new int[intersection.length];
    for (int i = 0; i < intersection.length; i++) {
        result[i] = intersection[i];
    }
    return result;
}

// by nested loop
public static int[] intersectionByLoop(int[] nums1, int[] nums2) {
    if(nums1.length < 1 || nums2.length < 1) {
        return new int[]{};
    }

    int[] shortArray = nums1;
    int[] longArray = nums2;
    if(nums1.length > nums2.length) {
        shortArray = nums2;
        longArray = nums1;
    }

    Set<Integer> intersectionSet = new HashSet<>();
    for(int i = 0; i < shortArray.length; i++) {
        for(int j = 0; j < longArray.length; j++) {
            if(shortArray[i] == longArray[j]) {
                intersectionSet.add(shortArray[i]);
            }
        }
    }

    return convertSetToArray(intersectionSet);
}

// by sort and queue
public static int[] intersectionBySort(int[] nums1, int[] nums2) {
    if(nums1.length < 1 || nums2.length < 1) {
        return new int[]{};
    }

    Arrays.sort(nums1);
    Arrays.sort(nums2);

    int[] shortArray = nums1;
    int[] longArray = nums2;
    if(nums1.length > nums2.length) {
        shortArray = nums2;
        longArray = nums1;
    }

    LinkedList<Integer> linkedList = new LinkedList();
    for(int i = 0; i < shortArray.length; i++) {
        linkedList.add(shortArray[i]);
    }

    Set<Integer> intersectionSet = new HashSet<>();
    for(int i = 0; i < longArray.length; i++) {
        if(linkedList.isEmpty()) {
            break;
        }

        if(linkedList.peek().equals(longArray[i])) {
            intersectionSet.add(linkedList.poll());
        } else if(longArray[i] > linkedList.peek()) {
            linkedList.poll();
            i--;
        }
    }

    return convertSetToArray(intersectionSet);
}

// by map
public static int[] intersectionByMap(int[] nums1, int[] nums2) {
    if(nums1.length < 1 || nums2.length < 1) {
        return new int[]{};
    }

    int[] shortArray = nums1;
    int[] longArray = nums2;
    if(nums1.length > nums2.length) {
        shortArray = nums2;
        longArray = nums1;
    }

    Map<Integer, Integer> intersectionMap = new HashMap<>();
    for(int i = 0; i < shortArray.length; i++) {
        if(!intersectionMap.containsKey(shortArray[i])) {
            intersectionMap.put(shortArray[i], 1);
        }
    }

    for(int i = 0; i < longArray.length; i++) {
        if(intersectionMap.containsKey(longArray[i])) {
            intersectionMap.put(longArray[i], 2);
        }
    }

    Set<Integer> set = new HashSet<>();
    for(Integer key : intersectionMap.keySet()) {
        if(intersectionMap.get(key) == 2) {
            set.add(key);
        }
    }

    return convertSetToArray(set);
}

Code details can be viewed by clicking My GitHub Warehouse

Topics: github