PHP gets the first non-repeating character in a character stream

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.