-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtop_one.py
More file actions
31 lines (26 loc) · 877 Bytes
/
Copy pathtop_one.py
File metadata and controls
31 lines (26 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Top one algorithm
:variable counter: This dictionary count the repeated elements in the array.
:variable max_reapeat: The value of this variable is the maximum repetition of the `counter`.
:variable result: The elements of this array save the repetitions of the `array` parameter.
e.g:
[1, 2, 1, 3, 4, 2, 2, 2]
counter = {'1': 2, '2': 4, '3': 1, '4': 1}
max_repeat = 4
result = [1, 2]
"""
def top_one(array: list) -> list:
result = []
counter = {}
max_repeat = 0
try:
for val in array:
if val not in counter:
counter[val] = 1
else:
counter[val] += 1
if val not in result:
result.append(val)
max_repeat = max(counter.values())
return result, max_repeat
except ValueError:
return ()