# easiest way to count the frequency of all elements in a list
lst = ['Sam', 'Sam', 'Tim', 'Tim', 'Tim', 'r', 'l']
freq = {} # stores the frequency of elements
counting = [freq.update({x: lst.count(x)}) for x in lst]
# output of freq
{'Sam': 2, 'Tim': 3, 'r': 1, 'l': 1}
#credit: buggyprogrammer.com
#Note: if you print "counting" it will return a list full of None so ignore it.
from collections import Counter
def frequency_table(n):
table = Counter(n)
print('Number Frequency')
for number in table.most_common() :
print('{0} {1}'.format(number[0], number[1]))
# src : Doing Math With Python
def countFreq(arr):
visited = [False for _ in range(len(arr))]
for i in range(len(arr)):
count = 1
if visited[i]:
continue
for j in range(i+1, len(arr)):
if arr[i] == arr[j]:
count += 1
visited[j] = True
visited[i] = True
print(f'{arr[i]} : {count}')
# Time complexity: O(n * n)
# space complexity: O(n)