Given two arrays, write a function to calculate 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]

Description:

Each element in the output must be unique. We can ignore the order of the output.

/ / the map method
var intersection = function(nums1, nums2) {
    let map = new Map(a); nums1.forEach(item= > {
        map.set(item, true)})let res = [];
    nums2.forEach(item= > {
        if(map.get(item)){ res.push(item); map.delete(item); }})return res;
};
/ / es6 method
var intersection = function(nums1, nums2) {
    let res = [];
    nums1.forEach(item= > {
        if(nums2.includes(item)){ res.push(item); }})return [...new Set(res)];
};
Copy the code