最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Finding the mode's of an array? Javascript - Stack Overflow

programmeradmin1浏览0评论

Okay, I've only figured out how to get one mode out of the array.. But I want to get 2, 3 or more if they occur the same amount of times. This is the code:

var frequency = {};  // array of frequency.
var maxFreq = 0;  // holds the max frequency.

for (var i in array) {
    frequency[array[i]] = (frequency[array[i]] || 0) + 1; // increment frequency.

    if (frequency[array[i]] > maxFreq) { // is this frequency > max so far ?
        maxFreq = frequency[array[i]];  // update max.
        mode = array[i];          // update result.
    }
}

So right now, if I've got a array = [3, 8, 3, 6, 1, 2, 9]; I get mode = 3;

But what I'm looking for is if array = [3, 6, 1, 9, 2, 3, 6, 6, 3, 1, -8, 7]; I want to get the mode = 3, 6;

Okay, I've only figured out how to get one mode out of the array.. But I want to get 2, 3 or more if they occur the same amount of times. This is the code:

var frequency = {};  // array of frequency.
var maxFreq = 0;  // holds the max frequency.

for (var i in array) {
    frequency[array[i]] = (frequency[array[i]] || 0) + 1; // increment frequency.

    if (frequency[array[i]] > maxFreq) { // is this frequency > max so far ?
        maxFreq = frequency[array[i]];  // update max.
        mode = array[i];          // update result.
    }
}

So right now, if I've got a array = [3, 8, 3, 6, 1, 2, 9]; I get mode = 3;

But what I'm looking for is if array = [3, 6, 1, 9, 2, 3, 6, 6, 3, 1, -8, 7]; I want to get the mode = 3, 6;

Share Improve this question edited Sep 18, 2015 at 9:45 Subramanyam M 3252 gold badges6 silver badges18 bronze badges asked Sep 18, 2015 at 9:10 Z.crazeZ.craze 91 silver badge2 bronze badges 7
  • possible duplicate of Counting the occurrences of JavaScript array elements – Etheryte Commented Sep 18, 2015 at 9:12
  • can you explain these sentences? "get one mode out of the array" "get the mode = 3, 6" thanks, – FoxInCloud Commented Sep 18, 2015 at 9:19
  • 1 Oh, sorry! I want to get the "mode" into a object. This is my object: return { max: max, mean: mean, median: median, min: min, mode: mode, range:range }; – Z.craze Commented Sep 18, 2015 at 9:42
  • 1 so I want the mode value to get into the object key (mode) – Z.craze Commented Sep 18, 2015 at 9:44
  • @Nit, not a duplicate. Link provided asks for the number of occurrences for each number instead of getting number that has maximum number of occurrences – Ramen_Lover912 Commented Mar 15, 2020 at 7:33
 |  Show 2 more ments

2 Answers 2

Reset to default 4

The question doesn't state how to get the modes, but if we want them in an array, we could change the code like this:

function getModes(array) {
  var frequency = []; // array of frequency.
  var maxFreq = 0; // holds the max frequency.
  var modes = [];

  for (var i in array) {
    frequency[array[i]] = (frequency[array[i]] || 0) + 1; // increment frequency.

    if (frequency[array[i]] > maxFreq) { // is this frequency > max so far ?
      maxFreq = frequency[array[i]]; // update max.
    }
  }

  for (var k in frequency) {
    if (frequency[k] == maxFreq) {
      modes.push(k);
    }
  }

  return modes;
}

alert(getModes([3, 6, 1, 9, 2, 3, 6, 6, 3, 1, -8, 7]));

function modeCount(data) {
  let modecount = [];
  let valueArr = [];
  let dataSet = new Set(data);
  for (const iterator of dataSet) {
    const filteredNum = data.filter((num) => iterator === num);
    modecount.push({
      mode: iterator,
      count: filteredNum.length
    });
  }

  modecount.sort((a, b) => {
    return b.count - a.count;
  });

  modecount.forEach(value => {
    if (value.count === modecount[0].count) {
      valueArr.push(value.mode);
    }
  });

  return valueArr;
}
let ages = [3, 6, 1, 9, 2, 3, 6, 6, 3, 1, -8, 7]
console.log(modeCount(ages));
发布评论

评论列表(0)

  1. 暂无评论