How to count the frequency of the elements in an unordered list?

后端 未结 30 2882
时光说笑
时光说笑 2020-11-22 02:37

I need to find the frequency of elements in an unordered list

a = [1,1,1,1,2,2,2,2,3,3,4,5,5]

output->

b =         


        
30条回答
  •  不思量自难忘°
    2020-11-22 02:45

    Yet another solution with another algorithm without using collections:

    def countFreq(A):
       n=len(A)
       count=[0]*n                     # Create a new list initialized with '0'
       for i in range(n):
          count[A[i]]+= 1              # increase occurrence for value A[i]
       return [x for x in count if x]  # return non-zero count
    

提交回复
热议问题