In this Leetcode Intersection of Two Arrays II problem solution you have given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.

Leetcode Intersection of Two Arrays II problem solution


Problem solution in Python.

class Solution:
    def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
        output = []
        for x in nums1:
            if x in nums2:
                output.append(x)
                nums2.remove(x)
        return output



Problem solution in Java.

public class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        if (nums1.length == 0 || nums2.length == 0) {
            return new int[]{};
        }
        HashMap<Integer, Integer> hMap = new HashMap<Integer, Integer>();
        int i;
        int value;
        for(i = 0; i < nums1.length; i++) {
            value = hMap.containsKey(nums1[i]) ? hMap.get(nums1[i])+1 : 1;
            hMap.put(nums1[i], value);
        }
        int size = nums1.length > nums2.length ? nums2.length : nums1.length;
        int[] intersection = new int[size];
        int j = 0;
        for(i = 0; i < nums2.length; i++) {
            value = hMap.containsKey(nums2[i]) ? hMap.get(nums2[i]) : 0; 
            if (value > 0) {
               intersection[j++] = nums2[i];
               hMap.put(nums2[i], value-1);
            }
        }
        int[] result = new int[j];
        for (i = 0; i < j; i++) {
            result[i] = intersection[i];
        }
        return result;
    }
}


Problem solution in C++.

class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        vector<int> res;
        unordered_map<int,int> num2cnt;
        for (auto i=0; i<nums1.size(); ++i) {
            num2cnt[nums1[i]] += 1;
        } 
        for (auto i=0; i<nums2.size(); ++i) {
            if (num2cnt[nums2[i]] >= 1) {
                num2cnt[nums2[i]] -= 1;
                res.push_back(nums2[i]);
            }
        }
        return res;
    }
};


Problem solution in C.

void swap(int *a, int *b) {
    int tmp = *a;
    *a = *b;
    *b = tmp;
}
  
void heapify(int arr[], int n, int i) {
    int max = i;
    int leftChild = 2 * i + 1;
    int rightChild = 2 * i + 2;
  
    if (leftChild < n && arr[leftChild] > arr[max])
        {max = leftChild;}
  
    if (rightChild < n && arr[rightChild] > arr[max])
        {max = rightChild;}
  
    if (max != i) {
        swap(&arr[i], &arr[max]);
        heapify(arr, n, max);
    }
}
  
void heapSort(int* arr, int n) {
    for (int i = n / 2 - 1; i >= 0; i--)
        heapify(arr, n, i);
  
    for (int i = n - 1; i >= 0; i--) {
        swap(&arr[0], &arr[i]);
        heapify(arr, i, 0);
    }
}


int* intersect(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
    if(nums1Size == 0 || nums2Size == 0) {
        *returnSize = 0;
        return (int*)malloc(0);
    }
    heapSort(nums1,nums1Size);
    heapSort(nums2,nums2Size);
    
    int* longArr;
    int longArrSize;
    int* shortArr;
    int shortArrSize;    
    
    if(nums1Size>nums2Size){
        longArr = nums1;
        shortArr = nums2;
        longArrSize = nums1Size;
        shortArrSize = nums2Size;
    } else {
        longArr = nums2;
        shortArr = nums1;
        longArrSize = nums2Size;
        shortArrSize = nums1Size;  
    }
    
    int count = 0;
    int tempReturnArr[shortArrSize];
    int p1 = 0;
    int p2 = 0;
    
    while(p1 < shortArrSize && p2 < longArrSize) {
        if(shortArr[p1]==longArr[p2]){
            tempReturnArr[count] = shortArr[p1];
            count ++;
            p1++;
            p2++;
        } else if (shortArr[p1]>longArr[p2]) {
            p2++;
        } else{
            p1++;
        }
    }
    
    int* returnArr = (int*)malloc(sizeof(int)*(count));
    for(int i = 0; i < count; i++) {
        returnArr[i] = tempReturnArr[i];
    }
    *returnSize = count;
    return returnArr;
}