FOR FREE CONTENT

Frequency of each element in the Array

Back to Programming

Description

The elements of the array are taken as input from the user. Then the frequency of each element is counted separately and printed.

 

Algorithm

INPUT: Array elements
OUTPUT: the frequency of each element
PROCESS:
Step 1: [taking the input]
	Read n [number of elements]
	For i=0 to n-1 repeat
		Read a[i]
		Set f[i]-1
	[end of ‘for’ loop]
Step 2: [Counting the frequency of the elements of array]
	For i=0 to n-1 repeat
        		Set c<- 1
        		For j=i+1 to n-1 repeat
If a[i]=a[j] then
                			Set c<-c+1
                			Set f[j]<-0
            			[End of ‘if’]
        		[End of ‘for’ loop]
        		If f[i]≠0 then
            			Set f[i]<-c
        		[End of ‘if’]
    	[End of ‘for’ loop]
    	Print "Frequency of the elements of array :  "
    	For i=0 to n-1 repeat
        		If f[i]≠0 then
           			 Print "The frequency of a[i] is f[i] "
        		[End of ‘if’]
    	[End of ‘for’ loop]
Step 3: Stop.

Code

Complexity: