Given a string , find its first non | Python program to print the first non - repeating character ; Returns an array of size 256 containing count of characters in the passed char array ; The function returns index of first non - repeating character in a string . If all characters are repeating then returns - 1 ; Driver program to test above function
NO_OF_CHARS = 256 NEW_LINE def getCharCountArray ( string ) : NEW_LINE INDENT count = [ 0 ] * NO_OF_CHARS NEW_LINE for i in string : NEW_LINE INDENT count [ ord ( i ) ] += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def firstNonRepeating ( string ) : NEW_LINE INDENT count = getCharCountArray ( string ) NEW_LINE index = - 1 NEW_LINE k = 0 NEW_LINE for i in string : NEW_LINE INDENT if count [ ord ( i ) ] == 1 : NEW_LINE INDENT index = k NEW_LINE break NEW_LINE DEDENT k += 1 NEW_LINE DEDENT return index NEW_LINE DEDENT string = " geeksforgeeks " NEW_LINE index = firstNonRepeating ( string ) NEW_LINE if index == 1 : NEW_LINE INDENT print " Either ▁ all ▁ characters ▁ are ▁ repeating ▁ or ▁ string ▁ is ▁ empty " NEW_LINE DEDENT else : NEW_LINE INDENT print " First ▁ non - repeating ▁ character ▁ is ▁ " + string [ index ] NEW_LINE DEDENT