FOR FREE CONTENT

Check one given element of an array present more than n/2 times or not.

Back to Programming

Description

The elements of the array are taken as input. One value is taken from the user and that value is searched in the array whether it appears more than n/2 times in the array or not, where n is the number of elements of the array.

 

Algorithm

INPUT: Array elements
OUTPUT: the frequency of an element more than n/2 or not
PROCESS:
Step 1: [taking the input]
	Read n [number of elements]
	For i=0 to n-1 repeat
		Read a[i]
	[end of ‘for’ loop]
	Read x [the element to search]
Step 2: [calculating the frequency of the element in array]
	Set c<-0
	For i=0 to n-1 repeat
		If a[i]=x then
			Set c<-c+1
		[End of ‘if’]
	[End of ‘for’ loop]
	If c>n/2 then
		Print “Yes, the element x is present more than n/2 times in the array”
	Else
		Print “No, the element x does not occur more than n/2 times in the array”
	[End of ‘if’]
Step 3: Stop.

Code

Complexity: