Last seen array element ( last appearance is earliest ) | Python3 program to find last seen element in an array . ; Returns last seen element in arr [ ] ; Store last occurrence index of every element ; Find an element in hash with minimum index value ; Driver code
import sys ; NEW_LINE def lastSeenElement ( a , n ) : NEW_LINE INDENT hash = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT hash [ a [ i ] ] = i NEW_LINE DEDENT res_ind = sys . maxsize NEW_LINE res = 0 NEW_LINE for x , y in hash . items ( ) : NEW_LINE INDENT if y < res_ind : NEW_LINE INDENT res_ind = y NEW_LINE res = x NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 1 , 2 , 2 , 4 , 1 ] NEW_LINE n = len ( a ) NEW_LINE print ( lastSeenElement ( a , n ) ) NEW_LINE DEDENT