This article mainly introduces the method of getting the first non-repeating character in the character stream of PHP, which involves the operation skills of PHP traversal and judgment for index array, and the friend who is interested in PHP can refer to this article
The example in this article describes how PHP obtains the first non-repeating character in a character stream. Share to everyone for your reference, as follows:
Problem
Implement a function to find the first character in a character stream that appears only once. For example, when you read only the first two characters "go" from a character stream, the first character that appears only once is "G". When the first six characters "Google" are read from the stream, the character that appears only once is "L".
Output Description:
Returns the # character if the current character stream does not exist once
Exercises
Using indexed arrays
Implementation code
<?phpglobal $result;//init module If you needfunction Init () { global $result; $result = [];} Insert one char from stringstreamfunction Insert ($ch) { global $result; Write code here if (isset ($result [$ch])) { $result [$ch]++; } else{ $result [$ch] =1; }} Return the first appearence once char in current stringstreamfunction firstappearingonce () { global $result; foreach ($result as $k = + $v) { if ($v ==1) { return $k; } } Return "#";}
The above is all the content of this article, I hope to learn from you to help!