Get the element with the highest occurrence in an array

后端 未结 30 1190
野性不改
野性不改 2020-11-22 11:17

I\'m looking for an elegant way of determining which element has the highest occurrence (mode) in a JavaScript array.

For example, in

[\'pear\', \'a         


        
相关标签:
30条回答
  • 2020-11-22 12:04

    Here is my way. I try to group data fist.

    const _ = require("underscore")
    
    var test  = [ 1, 1, 2, 1 ];
    var groupResult = _.groupBy(test, (e)=> e);
    

    The groupResult should be

    {
      1: [1, 1, 1]
      2: [2] 
    }
    

    Then find the property which has the longest array

    function findMax(groupResult){
       var maxArr = []
       var max;
       for(var item in groupResult){
         if(!max) { 
            max = { value:item, count: groupResult[item].length } ; 
            maxArr.push(max); 
            continue;
         }
         if(max.count < groupResult[item].length){ 
            maxArr = [];
            max = { value:item, count: groupResult[item].length }
            maxArr.push(max)
         } else if(max === groupResult[item].length)
            maxArr.push({ value:item, count: groupResult[item].length })
       }
       return maxArr;
    }
    

    The complete code looks like

    const _ = require("underscore")
    
    var test  = [ 1, 1, 2, 1 ];
    var groupResult= _.groupBy(test, (e)=> e);
    console.log(findMax(groupResult)[0].value);
    
    function findMax(groupResult){
       var maxArr = []
       var max;
       for(var item in groupResult){
         if(!max) { 
            max = { value:item, count: groupResult[item].length } ; 
            maxArr.push(max); 
            continue;
         }
         if(max.count < groupResult[item].length){ 
            maxArr = [];
            max = { value:item, count: groupResult[item].length }
            maxArr.push(max)
         } else if(max === groupResult[item].length)
            maxArr.push({ value:item, count: groupResult[item].length })
       }
       return maxArr;
    }
    
    0 讨论(0)
  • 2020-11-22 12:05
    var mode = 0;
    var c = 0;
    var num = new Array();
    var value = 0;
    var greatest = 0;
    var ct = 0;
    

    Note: ct is the length of the array.

    function getMode()
    {
        for (var i = 0; i < ct; i++)
        {
            value = num[i];
            if (i != ct)
            {
                while (value == num[i + 1])
                {
                    c = c + 1;
                    i = i + 1;
                }
            }
            if (c > greatest)
            {
                greatest = c;
                mode = value;
            }
            c = 0;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 12:05

    I guess you have two approaches. Both of which have advantages.

    Sort then Count or Loop through and use a hash table to do the counting for you.

    The hashtable is nice because once you are done processing you also have all the distinct elements. If you had millions of items though, the hash table could end up using a lot of memory if the duplication rate is low. The sort, then count approach would have a much more controllable memory footprint.

    0 讨论(0)
  • 2020-11-22 12:05

    You can try this:

     // using splice()   
     // get the element with the highest occurence in an array
        function mc(a) {
          var us = [], l;
          // find all the unique elements in the array
          a.forEach(function (v) {
            if (us.indexOf(v) === -1) {
              us.push(v);
            }
          });
          l = us.length;
          while (true) {
            for (var i = 0; i < l; i ++) {
              if (a.indexOf(us[i]) === -1) {
                continue;
              } else if (a.indexOf(us[i]) != -1 && a.length > 1) {
                // just delete it once at a time
                a.splice(a.indexOf(us[i]), 1);
              } else {
                // default to last one
                return a[0];
              }
            }
          }
        }
    
    // using string.match method
    function su(a) {
        var s = a.join(),
                uelms = [],
                r = {},
                l,
                i,
                m;
    
        a.forEach(function (v) {
            if (uelms.indexOf(v) === -1) {
                uelms.push(v);
            }
        });
    
        l = uelms.length;
    
        // use match to calculate occurance times
        for (i = 0; i < l; i ++) {
            r[uelms[i]] = s.match(new RegExp(uelms[i], 'g')).length;
        }
    
        m = uelms[0];
        for (var p in r) {
            if (r[p] > r[m]) {
                m = p;
            } else {
                continue;
            }
        }
    
        return m;
    }
    
    0 讨论(0)
  • 2020-11-22 12:06

    Based on Emissary's ES6+ answer, you could use Array.prototype.reduce to do your comparison (as opposed to sorting, popping and potentially mutating your array), which I think looks quite slick.

    const mode = (myArray) =>
      myArray.reduce(
        (a,b,i,arr)=>
         (arr.filter(v=>v===a).length>=arr.filter(v=>v===b).length?a:b),
        null)
    

    I'm defaulting to null, which won't always give you a truthful response if null is a possible option you're filtering for, maybe that could be an optional second argument

    The downside, as with various other solutions, is that it doesn't handle 'draw states', but this could still be achieved with a slightly more involved reduce function.

    0 讨论(0)
  • 2020-11-22 12:06

    Try it too, this does not take in account browser version.

    function mode(arr){
    var a = [],b = 0,occurrence;
        for(var i = 0; i < arr.length;i++){
        if(a[arr[i]] != undefined){
            a[arr[i]]++;
        }else{
            a[arr[i]] = 1;
        }
        }
        for(var key in a){
        if(a[key] > b){
            b = a[key];
            occurrence = key;
        }
        }
    return occurrence;
    }
    alert(mode(['segunda','terça','terca','segunda','terça','segunda']));
    

    Please note that this function returns latest occurence in the array when 2 or more entries appear same number of times!

    0 讨论(0)
提交回复
热议问题