if res remains Integer . MAX_VALUE , it means there are no characters that repeat only once or the string is empty
if ( res == Integer . MAX_VALUE ) return - 1 ; else return res ; } public static void main ( String args [ ] ) { String str ; str = " geeksforgeeks " ; int firstIndex = firstNonRepeating ( str ) ; if ( firstIndex == - 1 ) System . out . println ( " Either ▁ all ▁ characters ▁ are ▁ repeating ▁ or ▁ string ▁ is ▁ empty " ) ; else System . out . println ( " First ▁ non - repeating ▁ character ▁ is ▁ " + str . charAt ( firstIndex ) ) ; } }