Skip to content

Commit 4a8696f

Browse files
authored
Most frequent element in an array.
1 parent 403151b commit 4a8696f

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,5 +474,15 @@ el.addEventListener('click', myClickHandler, {
474474

475475
```
476476
477+
# Most frequent element in an array.
478+
```javascript
479+
const mostFrequent = arr =>
480+
Object.entries(
481+
arr.reduce((a, v) => {
482+
a[v] = a[v] ? a[v] + 1 : 1;
483+
return a;
484+
}, {})
485+
).reduce((a, v) => (v[1] >= a[1] ? v : a), [null, 0])[0];
477486

487+
```
478488

0 commit comments

Comments
 (0)