将数组中重复次数大于1的元素列出来(Map思路)
查找重复元素
https://www.nowcoder.com/practice/871a468deecf453589ea261835d6b78b
function duplicates(arr) {
let map = new Map();
let resultMap = arr.reduce((prev, curr) => {
return prev.set(curr, prev.get(curr) + 1 || 1);
}, map);
let list = [];
for (let [key, value] of resultMap) {
if (value > 1) {
list.push(key);
}
}
console.log(resultMap)
return list;
}
console.log(duplicates([1, 2, 4, 4, 3, 3, 1, 5, 3]))