I have a function that stores strings inside an array and after calculating which one is the most repeated it returns it to me. The problem is that now I also need to get the second most repeated and in case two elements are repeated the same number of times I need to get both elements. I have given it a lot of thought but I can't figure out how to do it.
I have tried to remove the most repeated element from the array once it has been returned and then run the same function again without that element, but I don't find it practical. I don't know if there is another way to do it, I hope you can help me, thank you very much in advance.
arrayTotal = [...result4, ...result7, ...result8, ...result10, ...result11, ...result12, ...result13, ...result14];
var arrayOrder = arrayTotal.sort();
console.log(arrayOrder);
var map = {};
var occurence = "";
var valor_mayor = 0;
if (typeof arrayOrder === "string") {
arrayOrder = arrayOrder.split(" ");
}
for (let elemento of arrayOrder) {
if (map[elemento]) {
map[elemento]++;
} else {
map[elemento] = 1;
}
};
for (let elemento in map) {
if (map[elemento] > valor_mayor) {
valor_mayor = map[elemento];
occurence = elemento;
}
}
console.log(valor_mayor);
console.log(occurence);