PHP Interview question: How to know an unknown length of a string which character appears the most times? (strings can be made up of letters, numbers, and other characters), write the code.

Source: Internet
Author: User
How do I know which character has the most occurrences of a string of unknown length? (strings can be made up of letters, numbers, and other characters), write the code.


$str = "asdfgfdas323344##$\ $fdsdfg *$**$*$**$$443563536254fas";//string of any length

Solution one (the quickest solution, but the basic skills should be solid)
$arr =str_split ($STR);
$arr =array_count_values ($arr);
Arsort ($arr);
Print_r ($arr);

Solution II (a certain requirement for logical ability)
$arr =str_split ($STR);
$con =array ();
foreach ($arr as $v) {
if ([email protected] $con [$v]) {
@ $con [$v]=1;
}else{
@ $con [$v]++;
}
}
Arsort ($con);
Print_r ($con);

Solution Three
$arr =str_split ($STR);
$unique =array_unique ($arr);
foreach ($unique as $a) {
$arr 2[$a]=substr_count ($str, $a);
}
Arsort ($arr 2);
Print_r ($arr 2);

PHP Interview question: How to know an unknown length of a string which character appears the most times? (strings can be made up of letters, numbers, and other characters), write the code.

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.