【leetcode】Intersection of Two Arrays

来源:互联网 发布:php serialize json 编辑:程序博客网 时间:2024/05/30 07:14

Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].

/** * @param {number[]} nums1 * @param {number[]} nums2 * @return {number[]} */var intersection = function(nums1, nums2) {    var is ={};    for(var i=0;i<nums1.length;i++){        is[nums1[i]]=0;    }    var res=[];    for(i=0;i<nums2.length;i++){        if(typeof is[nums2[i]]!=="undefined"){            is[nums2[i]]=1;        }    }    for(var num in is){        if(is[num]===1){            res.push(parseInt(num));        }    }    return res;};
0 0
原创粉丝点击