Given a string , find its first non | PHP program to find first non - repeating character ; calculate count of characters in the passed string ; The method returns index of first non - repeating character in a string . If all characters are repeating then returns - 1 ; Driver code
< ? php $ NO_OF_CHARS = 256 ; $ count = array_fill ( 0 , 200 , 0 ) ; function getCharCountArray ( $ str ) { global $ count ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) $ count [ ord ( $ str [ $ i ] ) ] ++ ; } function firstNonRepeating ( $ str ) { global $ count ; getCharCountArray ( $ str ) ; $ index = -1 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ count [ ord ( $ str [ $ i ] ) ] == 1 ) { $ index = $ i ; break ; } } return $ index ; } $ str = " geeksforgeeks " ; $ index = firstNonRepeating ( $ str ) ; if ( $ index == -1 ) echo " Either ▁ all ▁ characters ▁ are " . " ▁ repeating ▁ or ▁ string ▁ is ▁ empty " ; else echo " First ▁ non - repeating ▁ " . " character ▁ is ▁ " . $ str [ $ index ] ; ? >