Find the first repeating element in an array of integers | This function prints the first repeating element in arr [ ] ; This will set k = 1 , if any repeating element found ; max = maximum from ( all elements & n ) ; Array a is for storing 1 st time occurence of element initialized by 0 ; Store 1 in array b if element is duplicate initialized by 0 ; Duplicate element found ; Storing 1 st occurence of arr [ i ] ; Trace array a & find repeating element with min index ; Driver code
def printFirstRepeating ( arr , n ) : NEW_LINE INDENT k = 0 NEW_LINE max = n NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( max < arr [ i ] ) : NEW_LINE INDENT max = arr [ i ] NEW_LINE DEDENT DEDENT a = [ 0 for i in range ( max + 1 ) ] NEW_LINE b = [ 0 for i in range ( max + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ arr [ i ] ] ) : NEW_LINE INDENT b [ arr [ i ] ] = 1 NEW_LINE k = 1 NEW_LINE continue NEW_LINE DEDENT else : NEW_LINE INDENT a [ arr [ i ] ] = i NEW_LINE DEDENT DEDENT if ( k == 0 ) : NEW_LINE INDENT print ( " No ▁ repeating ▁ element ▁ found " ) NEW_LINE DEDENT else : NEW_LINE INDENT min = max + 1 NEW_LINE for i in range ( max + 1 ) : NEW_LINE INDENT if ( a [ i ] and ( min > ( a [ i ] ) ) and b [ i ] ) : NEW_LINE INDENT min = a [ i ] NEW_LINE DEDENT DEDENT print ( arr [ min ] ) NEW_LINE DEDENT DEDENT arr = [ 10 , 5 , 3 , 4 , 3 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE printFirstRepeating ( arr , n ) NEW_LINE