human
Most frequent element in an array | Python3 program to find the most frequent element in an array ..
Sort the array.
find the max frequency using linear traversal.
If last element is most frequent.
Driver Code"
How can the above be solved in Python?
 Can you provide a code to most frequent element in an array and the other steps required?
assistant
#Here is code to most frequent element in an array and the other steps required.
 def mostFrequent ( arr , n ) :
	arr . sort ( )
	max_count = 1 ; res = arr [ 0 ] ; curr_count = 1
	for i in range ( 1 , n ) :
		if ( arr [ i ] == arr [ i - 1 ] ) :
			curr_count += 1
		else :
			if ( curr_count > max_count ) :
				max_count = curr_count
				res = arr [ i - 1 ]
			curr_count = 1
	if ( curr_count > max_count ) :
		max_count = curr_count
		res = arr [ n - 1 ]
	return res
arr = [ 1 , 5 , 2 , 1 , 3 , 2 , 1 ]
n = len ( arr )
print ( mostFrequent ( arr , n ) )