Their thinking
- Use an empty object to record the number of digits in an array.
- Each time duplicate data is found, the number of recorded data in the object is subtracted by one and added to the result set. If the number is empty, it is not processed.
- Returns the result set.
code
/ * * *@param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}* /
var intersect = function (nums1, nums2) {
let data = {},
result = []
nums1.forEach((i) = > {
if (data[i]) {
data[i]++
} else {
data[i] = 1
}
})
nums2.forEach((j) = > {
if (data[j]) {
data[j]--
result.push(j)
}
})
return result
};
Copy the code