LeetCode #349 Intersection of Two Arrays
var intersection = function(nums1, nums2) {
let answer = [];
let arr1 = [...new Set(nums1)].sort((a, b) => a - b);
let arr2 = [...new Set(nums2)].sort((a, b) => a - b);
let p1 = 0, p2 = 0;
while(p1 < arr1.length && p2 < arr2.length) {
if(arr1[p1] === arr2[p2]) {
answer.push(arr1[p1++]);
p2++;
} else if(arr1[p1] < arr2[p2]) {
p1++;
} else {
p2++;
}
}
return answer;
};
// 다른 분의 풀이
function intersection(nums1, nums2) {
const set = new Set(nums1);
return [...new Set(nums2.filter(n => set.has(n)))];
}