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.

&nbsp;
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]
Explanation: [9,4] is also accepted.


&nbsp;
Constraints:


	1 &lt;= nums1.length, nums2.length &lt;= 1000
	0 &lt;= nums1[i], nums2[i] &lt;= 1000


&nbsp;
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?


class Solution:
    def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
        nums1.sort()
        nums2.sort()
        ans = []
        i, j = 0, 0
        while i < len(nums1) and j < len(nums2):
            if nums1[i] < nums2[j]:
                i += 1
            elif nums1[i] > nums2[j]:
                j += 1
            else:
                ans.append(nums1[i])
                i += 1
                j += 1
        return ans
class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        int[] dp = new int[1000+1];
        for(int i: nums1){
            dp[i]++;
        }
        int ptr =0;
        int ans[] = new int[1000+1];
        for(int i:nums2){
            if(dp[i]!= 0){
                ans[ptr]= i;
                ptr++;
                dp[i]--;
            }
                        
        }
        return Arrays.copyOfRange(ans,0,ptr);
    }
}
class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
     sort(nums1.begin(),nums1.end());
    sort(nums2.begin(),nums2.end());

    int a = nums1.size();
    int b = nums2.size();

    int i=0,j=0;
    vector<int>res;

    while(i<a && j<b)
    {
        if(nums1[i] > nums2[j])
        {
            j++;
        }
        else if(nums1[i] < nums2[j])
        {
            i++;
        }
        else
        {
            res.push_back(nums1[i]);
            i++;
            j++;
        }
    }

    return res;
}
};
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number[]}
 */
var intersect = function(nums1, nums2) {
    // initialize empty array
    let result = [];
    
    // sort arrays
    const nums1Sorted = nums1.sort((a,b) => a - b);
    const nums2Sorted = nums2.sort((a,b) => a - b);
    
    let i = 0;
    let j = 0;
    
    while(i < nums1Sorted.length && j < nums2Sorted.length ){
        // if nums1 index value is smaller than nums2 index value continue interating through nums1
       if(nums1Sorted[i] < nums2Sorted[j]){
           i++;
        // if nums1 index value is larger than nums2 index value continue interating through nums2
       }else if(nums1Sorted[i] > nums2Sorted[j]){
           j++;
       }else{
           // if match found, push to result
           result.push(nums1Sorted[i]);
           i++;
           j++;
       }
    }
    return result;
    
};
Intersection of Two Arrays II