How to count duplicate value in an array in javascript

前端 未结 28 1425
后悔当初
后悔当初 2020-11-22 06:07

Currently, I got an array like that:

var uniqueCount = Array();

After a few steps, my array looks like that:

uniqueCount =          


        
28条回答
  •  别那么骄傲
    2020-11-22 06:15

    By using array.map we can reduce the loop, see this on jsfiddle

    function Check(){
        var arr = Array.prototype.slice.call(arguments);
        var result = [];
        for(i=0; i< arr.length; i++){
            var duplicate = 0;
            var val = arr[i];
            arr.map(function(x){
                if(val === x) duplicate++;
            })
            result.push(duplicate>= 2);
        }
        return result;
    }
    

    To Test:

    var test = new Check(1,2,1,4,1);
    console.log(test);
    

提交回复
热议问题