My challenge is finding multiple modes from an array, but my code currently prints the modes multiple times. I specifically want to create an array with only 7 and 8 like this [7, 8] instead of [7, 7, 7, 7, 7, 8, 8, 8, 8, 8]. Can someone please assist me in fixing this issue?
const array = [1, 1, 2, 2, 2, 5, 5, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9];
let maxStreak = 0;
let modus = [];
for (let i = 0; i < array.length; i++) {
var series = 0;
for (let j = 0; j < array.length; j++) {
if (array[i] == array[j]) {
++series;
}
}
if (series > maxStreak) {
maxStreak = series;
modus = [];
modus.push(array[i]);
} else if (series == maxStreak) {
modus.push(array[i]);
console.log(modus);
}
}
I attempted a solution but still need it to output only 7 and 8 once.