After below loop , the value of arr [ x ] is going to be index of x if x appears only once . Else the value is going to be either - 1 or - 2.
for ( let i = 0 ; i < str . length ; i ++ ) { if ( arr [ str [ i ] . charCodeAt ( 0 ) ] == - 1 ) arr [ str [ i ] . charCodeAt ( 0 ) ] = i ; else arr [ str [ i ] . charCodeAt ( 0 ) ] = - 2 ; } let res = Number . MAX_VALUE ; for ( let i = 0 ; i < NO_OF_CHARS ; i ++ )