var intersection = function(nums1, nums2) {
const result = new Set();
const standardNums = nums1.length > nums2.length ? nums2 : nums1;
const longNums = nums1.length > nums2.length ? nums1 : nums2;
standardNums.forEach(num => {
longNums.forEach((longNum) => {
if(num === longNum){
result.add(num);
}
})
})
return [...result];
};
Runtime: 60 ms, faster than 98.75% of JavaScript online submissions for Intersection of Two Arrays.
Memory Usage: 34.8 MB, less than 56.82% of JavaScript online submissions for Intersection of Two Arrays.var intersection = function(nums1, nums2) { const set1 = new Set(nums1) const set2 = new Set(nums2) return [...set1].filter(n => set2.has(n)) };