判断数组中重复元素的个数

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次");
}

你可能感兴趣的:(判断数组中重复元素的个数)