One line ES6 solution. So many answers using object as a map but I can't see anyone using an actual Map
const map = arr.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());
Use map.keys()
to get unique elements
Use map.values()
to get the occurrences
Use map.entries()
to get the pairs [element, frequency]
var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]_x000D_
_x000D_
const map = arr.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());_x000D_
_x000D_
console.info([...map.keys()])_x000D_
console.info([...map.values()])_x000D_
console.info([...map.entries()])
_x000D_