【问题标题】:Highest occurrence in an array or first selected数组中出现次数最多的或首先选择的
【发布时间】:2017-03-17 13:36:45
【问题描述】:

我正在尝试获取数组值的最高出现次数,如果出现相同的情况,我应该获得相同出现次数的第一个选定值。

例子:

var array = ['25', '50', 'a', 'a', 'b', 'c']

在这种情况下,我应该得到a

var array = ['75', '100', 'a', 'b', 'b', 'a']

在这种情况下,我也应该得到a

我进行了相当多的搜索,发现了一些有用的帖子,例如:

不知何故,我似乎无法修改这些示例以适用于我的情况。

现在我正在使用下面的代码,但它返回最后一个选择的相等出现,而不是第一个。 (信用https://stackoverflow.com/users/1238344/emissary

function mostFrequent(array){
  return array.sort(function(a,b){
    return array.filter(function(v){ return v===a }).length
      - array.filter(function(v){ return v===b }).length
  }).pop();
}

欢迎对此提供任何帮助。

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    你可以这样使用:

    function mostFrequent(array) {
        var map = array.map(function(a) {
            return array.filter(function(b) {
                return a === b;
            }).length;
        });
    
        return array[map.indexOf(Math.max.apply(null, map))];
    }
    

    首先,它创建一个所有值出现的映射。接下来只需检查Math.max 哪个是最高的。检查indexOf 中出现次数最多的第一个值,并返回原始数组中该索引的值。

    ES2015

    如果 ES2015 是一个选项,你可以使用这个。它的代码更少。

    function mostFrequent(array) {
        let map = array.map((a) => array.filter((b) => a === b).length);
    
        return array[map.indexOf(Math.max.apply(null, map))];
    }
    

    如果您所在的地方甚至允许扩展运算符(NodeJS v5 及更高版本,Chrome 54),您可以将Math.max.apply(null, map) 替换为Math.max(...map)

    【讨论】:

      【解决方案2】:

      试试这个:

      const mostFrequent = arr => {
        let maxCount = 0;
        const occurrences = new Map();
        arr.forEach(x => {
          const count = occurrences.has(x) ? occurrences.get(x) + 1 : 1;
          occurrences.set(x, count);
          maxCount = count > maxCount ? count : maxCount;
        });
        return Array.from(occurrences).find(([element, count]) => count === maxCount)[0];
      };
      
      console.log(mostFrequent(['25', '50', 'a', 'a', 'b', 'c']));
      console.log(mostFrequent(['75', '100', 'a', 'b', 'b', 'a']));

      它与Get the element with the highest occurrence in an array中的解决方案基本相同,只有最后一行很重要:它不是返回最后一个maxElement,而是返回occurrences映射中与@具有相同count的第一个元素987654326@.

      【讨论】:

      • 感谢您的帮助,不胜感激。我接受了@daanvanham 的回答,因为他的代码对我来说更容易理解。
      【解决方案3】:

      除了给定的解决方案之外,这个提议的复杂度为 O(n) - 只有一个循环。

      基本上有两个对象,一个哈希表和一个结果集,通过迭代维护。

      function getValue(array) {
          var count = 0,
              index = -1;
      
          array.forEach(function (a, i) {
              this[a] = this[a] || { count: 0, index: i };
              this[a].count++;
              if (this[a].count > count) {
                  count = this[a].count;
                  index = this[a].index;
                  return;
              }
              if (this[a].count === count && this[a].index < index) {
                  index = this[a].index;
              }
          }, Object.create(null));
          return array[index];
      }
      
      console.log(getValue(['25', '50', 'a', 'a', 'b', 'c']));
      console.log(getValue(['25', '50', 'a', 'b', 'b', 'a', 'c']));

      【讨论】:

      • Nina Scholz 随时随地。
      【解决方案4】:

      已编辑:新的 jsfiddle。

      像这样:https://jsfiddle.net/Ldohv125/1/

      var array = ['75', '100', 'b', 'b', 'a', 'a'];
      var mf = 1;
      var m = 0;
      var item;
      for (var i=0; i<array.length; i++){
          for (var j=i; j<array.length; j++) {
              if (array[i] == array[j])
                  m++;
              if (mf<m){
                  mf=m; 
                  item = array[i];
              }
           }
           m=0;
      }
      alert(item);
      

      【讨论】:

      • I should get the first selected value of the equal occurrences. 不符合此条件
      • 我也没有。感谢您抽出宝贵时间帮助我,非常感谢。
      猜你喜欢
      • 2013-05-18
      • 1970-01-01
      • 2014-06-17
      • 2022-01-17
      • 2018-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      相关资源
      最近更新 更多