Count occurence in a list with time complexity of O(nlogn)

Viewed 4554

This is what I have so far:

alist=[1,1,1,2,2,3,4,2,2,3,2,2,1]
def icount(alist):
    adic={}
    for i in alist:
        adic[i]=alist.count(i)
    return adic

print(icount(alist))

I did some research to find out that the time complexity of list.count() is O(n), thus , this code will be O(n^2).

Is there a way to reduce this to O(nlogn)?

2 Answers
Related