Get the item that appears the most times in an array

后端 未结 12 1493
太阳男子
太阳男子 2020-11-27 19:18
var store = [\'1\',\'2\',\'2\',\'3\',\'4\'];

I want to find out that 2 appear the most in the array. How do I go about doing that?

12条回答
  •  温柔的废话
    2020-11-27 19:36

    This is my solution.

    var max_frequent_elements = function(arr){
    var a = [], b = [], prev;
    arr.sort();
    for ( var i = 0; i < arr.length; i++ ) {
        if ( arr[i] !== prev ) {
            a.push(arr[i]);
            b.push(1);
        } else {
            b[b.length-1]++;
        }
        prev = arr[i];
    }
    
    
    var max = b[0]
    for(var p=1;pmax)max=b[p]
     }
    
    var indices = []
    for(var q=0;q

    };

提交回复
热议问题