Home >>Python Programs >Python program to find the frequency of each element in the array
In this example, we will see a Python program in which we can find the frequency of each element in the given array.
Example :
#Initialize array
arr = [1, 2, 8, 4, 2, 2, 2, 5, 1, 2, 5, 3, 8, 7, 4, 1, 3, 5];
#Array fr will store frequencies of element
fr = [None] * len(arr);
visited = -1;
for i in range(0, len(arr)):
count = 1;
for j in range(i+1, len(arr)):
if(arr[i] == arr[j]):
count = count + 1;
#To avoid counting same element again
fr[j] = visited;
if(fr[i] != visited):
fr[i] = count;
#Displays the frequency of each element present in array
print("---------------------");
print(" Element | Frequency");
print("---------------------");
for i in range(0, len(fr)):
if(fr[i] != visited):
print(" " + str(arr[i]) + " | " + str(fr[i]));
print("---------------------");
--------------------- Element | Frequency --------------------- 1 | 3 2 | 5 8 | 2 4 | 2 5 | 3 3 | 2 7 | 1 ---------------------