Topic describes
Their thinking
- First, the array is deduplicated as a collection.
- Iterate over one of the arrays and add it to the result array if it is in another array.
- The result array is returned.
The problem solving code
var intersection = function(nums1, nums2) {
const temp1 = [...new Set(nums1)]
const temp2 = [...new Set(nums2)];
const result = [];
for (let v of temp1) {
if (temp2.includes(v)) {
result.push(v)
}
}
return result
};
Copy the code
The title to reflect
- Learn how to use sets to de-duplicate arrays.
- Learn to use includes to determine if an element exists in an array.