Mode | Function that sort input array a [ ] and calculate mode and median using counting sort . ; variable to store max of input array which will to have size of count array ; auxiliary ( count ) array to store count . Initialize count array as 0. Size of count array will be equal to ( max + 1 ) . ; Store count of each element of input array ; mode is the index with maximum count ; Driver Code
def printMode ( a , n ) : NEW_LINE INDENT max_element = max ( a ) NEW_LINE t = max_element + 1 NEW_LINE count = [ 0 ] * t NEW_LINE for i in range ( t ) : NEW_LINE INDENT count [ i ] = 0 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT count [ a [ i ] ] += 1 NEW_LINE DEDENT mode = 0 NEW_LINE k = count [ 0 ] NEW_LINE for i in range ( 1 , t ) : NEW_LINE INDENT if ( count [ i ] > k ) : NEW_LINE INDENT k = count [ i ] NEW_LINE mode = i NEW_LINE DEDENT DEDENT print ( " mode ▁ = ▁ " , mode ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 4 , 1 , 2 , 7 , 1 , 2 , 5 , 3 , 6 ] NEW_LINE n = len ( a ) NEW_LINE printMode ( a , n ) NEW_LINE DEDENT