I have an array that I want to get the most occurring elements,
First scenario
let arr1 = [\'foo\', \'foo\', \'foo\', \
You can also use a for ofloop and ìn
For example
const arrayFrecuent = [3, 1, 2, 1, 3, 2, 5, 4, 2, 10];
const mostFrecuent = givenArray => {
let counts = {};
let maxValue = -1;
let maxItem = null;
for (const num of givenArray) {
if (!(num in counts)) {
counts[num] = 1;
} else {
counts[num] = counts[num] + 1;
}
if (counts[num] > maxValue) {
maxValue = counts[num];
maxItem = num;
}
}
return maxItem;
};
const mostFrecuentNumber = mostFrecuent(arrayFrecuent);
console.log("mostFrecuentNumber", mostFrecuentNumber);