JS 找出数组中元素重复次数最多的元素

[1,5,6,2,4,6,8,9,3]找出数组中元素重复次数最多的元素

 

function getMaxRepCount(arr){

let newArr = arr.reduce(function(acc,cur,curIndex){

if(acc[cur]){

acc[cur]++;

} else {

acc[cur] = 1;

}

return acc;

},[])

 

let maxCount = 0;

let max = 0;

for(let x in newArr){

if(newArr[x]>maxCount){

max = x;

maxCount = newArr[x];

}

}

return max;

}

let arr6 = [1,5,6,2,4,6,8,9,3];

console.log(getMaxRepCount(arr6));

你可能感兴趣的:(js)