I have an array that I want to get the most occurring elements,
First scenario
let arr1 = ['foo', 'foo', 'foo', 'bar', 'bar', 'bar', 'baz', 'baz']
let newArr = someFunc(arr1)
so in this case I want the new array to have the value
console.log(newArr) // ['foo', 'bar']
Because the value 'foo' and 'bar' was the most occurring element of the array
Second scenario
let arr2 = ['foo', 'foo', 'foo', 'bar', 'baz']
let newArr = someFunc(arr2)
so in this case I want the new array to have the value
console.log(newArr) // ['foo']
Because the value 'foo' was the most occurring element of the array
This is what I have tried and it will only get me one of the elements even if there are more than one element that occurs the same amount of times
newArr= arr.sort((a,b) =>
arr.filter(v => v===a).length
- arr.filter(v => v===b).length
).pop()