Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Most common element - update for #29 #32

Merged
merged 5 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions src/listwiz/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

"""

from collections import Counter


def largest_element(l):
"""Find the largest element and its index
Expand Down Expand Up @@ -55,13 +57,6 @@ def most_common_element(ll):
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
c = Counter(ll)
return c.most_common(1)[0][0]

2 changes: 0 additions & 2 deletions tests/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,3 @@ def test_most_common_element():
ll = [1, 2, 3, 4, 1, 1, 2, 3, 3, 3, 3, 3]
res = lwi.most_common_element(ll)
assert res == 3