xxxxxxxxxx
# Python program to count the frequency of
# elements in a list using a dictionary
def CountFrequency(my_list):
# Creating an empty dictionary
freq = {}
for items in my_list:
freq[items] = my_list.count(items)
#Print:
for key, value in freq.items():
print ("% d : % d"%(key, value))
#Output:
1 : 5
2 : 4
3 : 3
4 : 3
5 : 2
xxxxxxxxxx
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)