assume
[1,2,4,4,3,3,3]
and upper_limit is 8 (all numbers are not bigger than upper_limit)
how to produce 3 under the help of upper_limit must run in O(n+upper_limit) overall time.
assume
[1,2,4,4,3,3,3]
and upper_limit is 8 (all numbers are not bigger than upper_limit)
how to produce 3 under the help of upper_limit must run in O(n+upper_limit) overall time.
 
    
    a = [1,1,2,2,2,3,3,4]
cnt = {}
most = a[0]
for x in a:
    if x not in cnt:
        cnt[x] = 0
    cnt[x] += 1
    if cnt[x] > cnt[most]:
        most = x
print(most)
 
    
    You can use collections.Counter
from collections import Counter
lst = [1,2,4,4,3,3,3]
counter = Counter(lst)
most_freq, freq = counter.most_common()[0]
Alternatives to Counter using dictionary. 
from collections import defaultdict
d = defaultdict(int)
lst = [1,2,4,4,3,3,3]
for val in lst:
    d[val] = d[val] + 1
most_freq, freq = max(d.items(), key = lambda t: t[1])
This example keeps track of the most frequent as you iterate through circumventing the need for max function.
from collections import defaultdict
d = defaultdict(int)
lst = [1,2,4,4,3,3,3]
most_freq, freq = None, 0
for val in lst:
    d[val] = d[val] + 1
    if d[val] > freq:
        most_freq = val
        freq = d[val]
print(most_freq, freq)
If you don't want to use a defaultdict then you can just use a normal dictionary and use dict.setdefault instead.
d = {} # set d to dict
d[val] = d.setdefault(val, 0) + 1
