I have an array that I want to get the most occurring elements,
First scenario
let arr1 = [\'foo\', \'foo\', \'foo\', \
You can calculate the max for each of the values and only return those which match via grouping them with an Array.reduce:
const mostFrequent = data => data.reduce((r,c,i,a) => {
r[c] = (r[c] || 0) + 1
r.max = r[c] > r.max ? r[c] : r.max
if(i == a.length-1) {
r = Object.entries(r).filter(([k,v]) => v == r.max && k != 'max')
return r.map(x => x[0])
}
return r
}, {max: 0})
console.log(mostFrequent(['foo', 'foo', 'foo', 'bar', 'bar', 'bar', 'baz', 'baz']))
console.log(mostFrequent(['foo', 'foo', 'foo', 'bar', 'baz']))