Finding the frequency of elements in an array using Python

Finding the frequency of elements in an array using Python

Finding the frequency of elements in an array

In this paragraph, we will learn how to code in Python. Here we have an array of elements to count the frequency or occurrence of its each element entered by the user. One of the approaches to resolve this problem is to maintain one array to store the counts of each element of the array then iterate loop through the array and count the frequency of each element and store it in another array.

For example:
Enter the size of array: 5
Enter the elements:
3   31  3
The occurrence of 2 is 2
The occurrence of 3 is 2
The occurrence of 31 is 1

Working required for finding the frequency of list using Python

STEP 1: Declare and Initialize an array.

STEP 2: Take the input in the array.

STEP 3: Make another array to store the frequency of elements.

STEP 4: Than Traverse the input array and update the count of the elements in the frequency array.

STEP 5: Now print the frequency array which displays the frequency of all the elements of the array

 

Finding the frequency of elements in an array using python

n=int(input("ENTER ARRAY SIZE "))

arr=[]

print("Enter the array elements")

for i in range(n):

    arr.append(int(input()))

x=list(dict.fromkeys(arr))

for i in x:

    print("\n {} occurs {} time(s)".format(i,arr.count(i)))
Output
Enter ARRAY SIZE 6
Enter the array elements
4
33
199
9
33
199

4 occures 1 time(s)
33 occures 2 time(s)
199 occures 2 time(s)
9 occures 1 time(s)