关于 javascript: 数组中出现次数最多或第一次被选中

Highest occurrence in an array or first selected

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

示例:

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

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

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

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

我已经进行了相当多的搜索并找到了一些有用的帖子,例如:

  • 获取数组中出现次数最多的元素

  • 找出数组中出现频率最高的项(不仅仅是字符串)

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

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

1
2
3
4
5
6
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();
}

欢迎对此提供任何帮助。


你可以这样使用:

1
2
3
4
5
6
7
8
9
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 是一个选项,你可以使用这个。它的代码更少。

1
2
3
4
5
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)


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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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']));


试试这个:

1
2
3
4
5
6
7
8
9
10
11
12
13
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']));

它与获取数组中出现次数最多的元素基本相同,只有最后一行有意义:它返回occurrences映射中的第一个元素,而不是返回最后一个maxElementmaxCount.

相同的 count


编辑:新的jsfiddle。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);