forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtop_1.py
More file actions
32 lines (25 loc) · 903 Bytes
/
top_1.py
File metadata and controls
32 lines (25 loc) · 903 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
32
"""
this algorithms receive array and check most_frequent_value(a.k.a mode). Also, sometimes it can be have numerous most_frequent_value,
so this funtion returns list. This result can be used as finding representative value on array.
This algorithms get array, and make dictionary of it, find most frequent count, and make result list.
For example) top_1([1, 1, 2, 2, 3, 4]) will return [1, 2]
Complexity: O(n)
"""
def top_1(arr):
values = {}
#reserve each value which first appears on keys
#reserve how many time each value appears by index number on values
result = []
f_val = 0
for i in arr:
if i in values:
values[i] += 1
else:
values[i] = 1
f_val = max(values.values())
for i in values.keys():
if values[i] == f_val:
result.append(i)
else:
continue
return result