I have an array that I want to get the most occurring elements,
First scenario
let arr1 = [\'foo\', \'foo\', \'foo\', \
You can count the items with reduce
and find the maximum occurring count. Then you can filter any keys that have that count:
let arr = ['foo', 'foo', 'foo', 'bar', 'bar', 'bar', 'baz', 'baz'];
let counts = arr.reduce((a, c) => {
a[c] = (a[c] || 0) + 1;
return a;
}, {});
let maxCount = Math.max(...Object.values(counts));
let mostFrequent = Object.keys(counts).filter(k => counts[k] === maxCount);
console.log(mostFrequent);