Prepare repository for merge
This commit is contained in:
21
python/snippets/most-frequent.md
Normal file
21
python/snippets/most-frequent.md
Normal file
@ -0,0 +1,21 @@
|
||||
---
|
||||
title: Most frequent element
|
||||
type: snippet
|
||||
tags: [list]
|
||||
cover: secret-tree
|
||||
dateModified: 2020-11-02T19:28:27+02:00
|
||||
---
|
||||
|
||||
Returns the most frequent element in a list.
|
||||
|
||||
- Use `set()` to get the unique values in `lst`.
|
||||
- Use `max()` to find the element that has the most appearances.
|
||||
|
||||
```py
|
||||
def most_frequent(lst):
|
||||
return max(set(lst), key = lst.count)
|
||||
```
|
||||
|
||||
```py
|
||||
most_frequent([1, 2, 1, 2, 3, 2, 1, 4, 2]) #2
|
||||
```
|
||||
Reference in New Issue
Block a user