题目描述
找出数组 arr 中重复出现过的元素
示例1
输入
[1, 2, 4, 4, 3, 3, 1, 5, 3]
输出
[1, 3, 4]
function duplicates(arr) {
var obj = {};
var repeatList = [];
//遍历数组,将数组的值作为obj的索引,出现次数为值
arr.forEach(function(item){
if(obj[item]){
obj[item] +=1;
}else{
obj[item] = 1;
}
});
//获取对象自身属性
var propertyNames = Object.getOwnPropertyNames(obj);
//遍历对象,将重复出现的元素取出
propertyNames.forEach(function(item){
if(obj[item] > 1){
repeatList.push(parseInt(item));
}
});
return repeatList;
}