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;
- 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
2 Answers
Reset to default 4The 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));