human
Write me a code to majority Element and the other steps required given the following:
Majority Element | Function to find Majority element in an array it returns - 1 if there is no majority element.
sort the array in O ( nlogn ).
increases the count if the same element occurs otherwise starts counting new element.
sets maximum count and stores maximum occured element so far if maximum count becomes greater than n / 2 it breaks out setting the flag.
returns maximum occured element if there is no such element , returns - 1.
Driver code.
Function calling"
Solution in Python:
assistant
#Here is code to majority Element and the other steps required.
 def majorityElement ( arr , n ) :
	arr . sort ( )
	count , max_ele , temp , f = 1 , - 1 , arr [ 0 ] , 0
	for i in range ( 1 , n ) :
		if ( temp == arr [ i ] ) :
			count += 1
		else :
			count = 1
			temp = arr [ i ]
		if ( max_ele < count ) :
			max_ele = count
			ele = arr [ i ]
			if ( max_ele > ( n // 2 ) ) :
				f = 1
				break
	if f == 1 :
		return ele
	else :
		return - 1
arr = [ 1 , 1 , 2 , 1 , 3 , 5 , 1 ]
n = len ( arr )
print ( majorityElement ( arr , n ) )