Mode in a stream of integers ( running integers ) | Function that prints the Mode values ; Map used to mp integers to its frequency ; To store the maximum frequency ; To store the element with the maximum frequency ; Loop used to read the elements one by one ; Updates the frequency of that element ; Checks for maximum Number of occurrence ; Updates the maximum frequency ; Updates the Mode ; Driver Code ; Function call
def findMode ( a , n ) : NEW_LINE INDENT mp = { } NEW_LINE max = 0 NEW_LINE mode = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] in mp : NEW_LINE INDENT mp [ a [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ a [ i ] ] = 1 NEW_LINE DEDENT if ( mp [ a [ i ] ] >= max ) : NEW_LINE INDENT max = mp [ a [ i ] ] NEW_LINE mode = a [ i ] NEW_LINE DEDENT print ( mode , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 2 , 7 , 3 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE findMode ( arr , n ) NEW_LINE