DEV Community

Discussion on: Find the Duplicate Number

Collapse
melvincarvalho profile image
Melvin Carvalho

1loc.dev/

const countOccurrences = arr => arr.reduce((prev, curr) => (prev[curr] = ++prev[curr] || 1, prev), {});

// Examples
countOccurrences([2, 1, 3, 3, 2, 3]); // { '1': 1, '2': 2, '3': 3 }
countOccurrences(['a', 'b', 'a', 'c', 'a', 'b']); // { 'a': 3, 'b': 2, 'c': 1 }

Collapse
ashking profile image
Ashwin Author

This is good!

Collapse
melvincarvalho profile image
Melvin Carvalho

Do look at the other functions in 1loc.dev

I liked this trick:

++prev[curr] || 1