js面试题——找到数组中的重复元素并判断重复次数且输出重复元素

var countArr = [1,2,3,4,5,6,3,4,3,3,7,8,9,32,1,11,2,3,3,3];
var res = {};
var maxnum=0;
var max;
function getRepeat(arr){
for (var i = 0; i < arr.length; i++) {
if(!res[arr[i]]){
res[arr[i]] = 1;
}else{
res[arr[i]] ++;
}
};
var keys = Object.keys(res);
console.log(keys);
for(var j=0;j<keys.length;j++){
if(res[keys[j]] > maxnum){
maxnum = res[keys[j]];
max = keys[j];
}
}
return maxnum + max
}
getRepeat(countArr);
console.log('出现次数最多的是' + max + '次数为' + maxnum);

输出结果:出现次数最多的是3次数为7

上一篇:【UVA - 10815】Andy's First Dictionary (set)


下一篇:Calling / Running a report in Oracle forms 10g / 11g