Find the first repeating element in an array of integers | This function prints the first repeating element in arr [ ] ; Initialize index of first repeating element ; Creates an empty hashset ; Traverse the input array from right to left ; If element is already in hash set , update min ; Else add element to hash set ; Print the result ; Driver code
function printFirstRepeating ( arr ) { let min = - 1 ; let set = new Set ( ) ; for ( let i = arr . length - 1 ; i >= 0 ; i -- ) { if ( set . has ( arr [ i ] ) ) min = i ; else set . add ( arr [ i ] ) ; } if ( min != - 1 ) document . write ( " " + arr [ min ] ) ; else document . write ( " " ) ; } let arr = [ 10 , 5 , 3 , 4 , 3 , 5 , 6 ] ; printFirstRepeating ( arr ) ;