Skip to content

Commit c7321c0

Browse files
committed
write top one algorithm
1 parent 54e523a commit c7321c0

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

arrays/top_one.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
top_one([1, 1, 2, 2, 3, 4]) => [1, 2]
3+
complexity: O(n)
4+
"""
5+
6+
7+
def top_one(array: list) -> list:
8+
values = {}
9+
for item in array:
10+
values[item] = values.get(item, 0) + 1
11+
max_repetition = max(values.values())
12+
result = []
13+
for key in values.keys():
14+
if values[key] == max_repetition:
15+
result.append(key)
16+
return result
17+
18+
19+
print(top_one([1, 1, 2, 2, 3, 4]))

0 commit comments

Comments
 (0)