判断数组中重复元素的个数
2019-06-19 本文已影响0人
干炸里脊不如你
function isRepeat(result,valueNum){ //判断数组中重复元素的个数
var arr = [];
result.sort()
for (var i = 0; i < result.length;) {
var count = 0;
for (var j = i; j < result.length; j++) {
if (result[i] === result[j]) {
count++;
}
}
arr.push({
value: result[i],
count: count
})
i+=count;
}
var countNum;
for (var k = 0; k < arr.length; k++) {
if(arr[k].value==valueNum){
countNum=arr[k].count
}
}
return countNum
}
var arr=[1,3,4,5,6,5,5]
if(isRepeat(arr,5)==3){
console.log("我出现了ji次");
}