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;

Subramanyam M
  • 335
  • 2
  • 6
  • 18
Z.craze
  • 9
  • 1
  • 2
  • possible duplicate of [Counting the occurrences of JavaScript array elements](http://stackoverflow.com/questions/5667888/counting-the-occurrences-of-javascript-array-elements) – Etheryte Sep 18 '15 at 09:12
  • can you explain these sentences? "get one mode out of the array" "get the mode = 3, 6" thanks, – FoxInCloud Sep 18 '15 at 09: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 Sep 18 '15 at 09:42
  • 1
    so I want the mode value to get into the object key (mode) – Z.craze Sep 18 '15 at 09: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 Mar 15 '20 at 07:33
  • @Ramen_Lover912 The question and the solution are the same once you consider what you actually need to do. – Etheryte Mar 15 '20 at 17:37
  • after running the code just use a `for in` loop to check for equal occurrences – Ramen_Lover912 Apr 12 '20 at 05:54

2 Answers2

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]));
Rich
  • 3,156
  • 3
  • 19
  • 29
Amit
  • 45,440
  • 9
  • 78
  • 110
  • 1
    the things is that I want to get the modes into an object, like this: return { max: max, mean: mean, median: median, min: min, mode: [mode], range:range }; – Z.craze Sep 18 '15 at 09:22
  • 1
    so how do I pick out modes from the function, best way? Cheers! – Z.craze Sep 18 '15 at 09:24
  • 1
    The function returns an array of modes. that's what you're asking for. – Amit Sep 18 '15 at 09:32
  • 1
    yea, I know. But how to get the array into my object so it looks like this instead return { max: max, mean: mean, median: median, min: min, mode: [3, 6], range:range }; – Z.craze Sep 18 '15 at 09:41
  • 1
    Okay, I get it! :) But, the problem is now that I get it as a string,,,I want the [3, 6] as number, How can I fix that problem? Cheers! – Z.craze Sep 18 '15 at 10:38
  • 1
    The function doesn't return a string, it returns an array. I think it would be wise of you to take a little time and learn JS from the ground up as you seem to be confusing a lot of things here. – Amit Sep 18 '15 at 14:16
0
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));