Repeated Character Whose First Appearance is Leftmost | Javascript program to find first repeating character ; The function returns index of the first repeating character in a string . If all characters are repeating then returns - 1 ; Mark all characters as not visited ; Traverse from right and update res as soon as we see a visited character . ; Driver Code
let NO_OF_CHARS = 256 ; function firstRepeating ( str ) { let visited = new Array ( NO_OF_CHARS ) ; for ( let i = 0 ; i < NO_OF_CHARS ; i ++ ) visited [ i ] = false ; let res = - 1 ; for ( let i = str . length - 1 ; i >= 0 ; i -- ) { if ( visited [ str [ i ] . charCodeAt ( ) ] == false ) visited [ str [ i ] . charCodeAt ( ) ] = true ; else res = i ; } return res ; } let str = " " ; let index = firstRepeating ( str ) ; if ( index == - 1 ) document . write ( " " + " " ) ; else document . write ( " " + " " + str [ index ] ) ;