human
Sorting element of an array by frequency in decreasing order | Function that return the index upto all the array elements are updated ..
Initialise maxE = - 1.
Find the maximum element of arr [ ].
Create frequency array freq [ ].
Update the frequency array as per the occurrence of element in arr [ ].
Initialise cnt to 0.
Traversing freq [ ].
If freq of an element is greater than 0 update the value of arr [ ] at index cnt & increment cnt.
Return cnt.
Function that print array arr [ ] elements in sorted order.
Traversing arr [ ] till index cnt.
Find frequency of elements.
Find value at index i.
Traversing till frequency to print value at index i.
Driver code.
Size of array arr [ ].
Function call to get cnt.
Sort the arr [ ] in decreasing order.
Function that prints elements in decreasing order"
How can the above be solved in Python?
 ... What is a code to sorting element of an array by frequency in decreasing order and the other steps required?
assistant
#Here is code to sorting element of an array by frequency in decreasing order and the other steps required.
 def sortByFreq ( arr , n ) :
	maxE = - 1 ;
	for i in range ( n ) :
		maxE = max ( maxE , arr [ i ] )
	freq = [ 0 ] * ( maxE + 1 ) ;
	for i in range ( n ) :
		freq [ arr [ i ] ] += 1 ;
	cnt = 0 ;
	for i in range ( maxE + 1 ) :
		if ( freq [ i ] > 0 ) :
			value = 100000 - i ;
			arr [ cnt ] = 100000 * freq [ i ] + value ;
			cnt += 1 ;
	return cnt ;
def printSortedArray ( arr , cnt ) :
	for i in range ( cnt ) :
		frequency = arr [ i ] / 100000 ;
		value = 100000 - ( arr [ i ] % 100000 ) ;
		for j in range ( int ( frequency ) ) :
			print ( value , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 4 , 4 , 5 , 6 , 4 , 2 , 2 , 8 , 5 ]
	n = len ( arr )
	cnt = sortByFreq ( arr , n ) ;
	arr . sort ( reverse = True )
	printSortedArray ( arr , cnt ) ;