字典:顾名思义,像新华字典一样可查找,基于键值对来存储
function intersection (nums1: number[], nums2: number[]): number[] {
const map = new Map();
nums1.forEach(n => {
map.set(n, true);
});
let res = [];
nums2.forEach(n => {
// 匹配
if(map.get(n)) {
res.push(n);
map.delete(n); // 紧接着移除改字典,因为已经用过了
}
});
return res;
}
const m = new Map();
// 增、改
m.set('a', 1); // 增加
m.set('b', true); // 增加
m.set('a', 2); // 修改
// 查
m.get('a');
// 删
m.delete('a');
m.clear();