See if there's a better way.

Source: Internet
Author: User
Just before the tech group saw an n-Bell question:
How to find the most occurrences of characters in a string using PHP

My idea is to first set the string to the number of groups, through Array_count_values to get the count of elements, sort, take the topmost one for the maximum number of times, then there may be more than the same maximum number of times, for Loop to find out:

$testStr = ' Rewruo ewjrewm '. Php_eol. ' Hcywer Gfaaf D State of China rew '; $testStr = preg_replace ('/\s/', ' ', $testStr);p reg_match_all ('/./u ', $testStr, $strList); $charCount = array_count_values ($strList [0]); Arsort ($charCount); $maxCount = current ($charCount); foreach ($charCount as $char = = $count) {if ($count < $maxCount) {break;} Echo $char. ' appeared '. $count. ' Times
';}


Reply to discussion (solution)

Since all use Array_count_values, with Max to find the intersection is, do not have to loop

You are arsort, direct echo key ($charCount); Is the most characters that appear.

Since all use Array_count_values, with Max to find the intersection is, do not have to loop




You are arsort, direct echo key ($charCount); Is the most characters that appear.


I loop in order to also lose the other characters that are the most number of times. Only one of them has been exported, according to your practice.


Since all use Array_count_values, with Max to find the intersection is, do not have to loop




You are arsort, direct echo key ($charCount); Is the most characters that appear.


I loop in order to also lose the other characters that are the most number of times. Only one of them has been exported, according to your practice.


You haven't tried, have you? Intersection Ah, Array_intersect, first try to see the results ~

You haven't tried, have you? Intersection Ah, Array_intersect, first try to see the results ~


To find the code, my Code looks for the following results:

R has appeared 5 times
W has appeared 5 times
The country has appeared 5 times
E has appeared 5 times

Because there are 4 characters that are the same number and are the maximum number of times

There's no room for simplification.

$testStr = ' Rewruo ewjrewm '. Php_eol. ' Hcywer Gfaaf D State of China rew '; $testStr = preg_replace ('/\s/', ' ', $testStr);p reg_match_all ('/./u ', $testStr, $strList); $strList = array_count_values ($strList [0]), $r = Array_keys ($strList, $m = max ($strList)), echo join ($t = "occurs $m times
", $r). $t;
R has appeared 5 times
E has appeared 5 times
W has appeared 5 times
The country has appeared 5 times
  • Related Article

    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.