Skip to content

Commit

Permalink
First version for most_common_element
Browse files Browse the repository at this point in the history
  • Loading branch information
gudlot committed Aug 27, 2024
1 parent 6e08cbe commit 961f87e
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/listwiz/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ def longest_decreasing_streak(l):
raise NotImplementedError


def most_common_element(l):
# Find the most common element in the list
raise NotImplementedError
def most_common_element(ll):
"""Find the most common element in a list.
Parameters
----------
ll : list
Returns
-------
most common element
"""

if not isinstance(ll, list):
raise ValueError("Input must be a list")

counting ={}
for i in ll:
if i in counting:
counting[i] += 1
else:
counting[i] = 1

# Find the key with the maximum value
most_common = max(counting, key=counting.get)
return most_common

0 comments on commit 961f87e

Please sign in to comment.