Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2]
.
Note:
- Each element in the result must be unique.
- The result can be in any order.
Tags
Binary Search, Hash Table, Two Pointers, Sort
--------------------------------------------- --------------------------------------------- --------------------------------------------- -------------------------------------Easy One
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
*/
var intersection = function(nums1, nums2) {
var len1 = nums1.length;
var len2 = nums2.length;
if(len1 < len2) {
return intersection(nums2,nums1);
}
//nums1 is larger than nums2
var ret = [];
for(var i=0;i<len2;i++) {
if(nums1.includes(nums2[i]) && !ret.includes(nums2[i])) {
ret.push(nums2[i]);
}
}
return ret;
};