php面试题:怎么知道一个未知长度的字符串哪个字符出现的次数最多?(字符串可由字母,数字及其他字符组成)请写出代码

WBOY
Release: 2016-06-13 12:17:51
Original
824 people have browsed it

php面试题:如何知道一个未知长度的字符串哪个字符出现的次数最多?(字符串可由字母,数字及其他字符组成)请写出代码。
如何知道一个未知长度的字符串哪个字符出现的次数最多?(字符串可由字母,数字及其他字符组成)请写出代码。


$str="asdfgfdas323344##$\$fdsdfg*$**$*$**$$443563536254fas";//任意长度字符串

//解法一(最快速的解法,但是基本功要扎实)
$arr=str_split($str);
$arr=array_count_values($arr);
arsort($arr);
print_r($arr);

//解法二(对逻辑能力有一定要求)
$arr=str_split($str);
$con=array();
foreach ($arr as $v){
    if (!@$con[$v]){
        @$con[$v]=1;
    }else{
        @$con[$v]++;
    }
}
arsort($con);
print_r($con);

//解法三
$arr=str_split($str);
$unique=array_unique($arr);
foreach ($unique as $a){
    $arr2[$a]=substr_count($str, $a);
}
arsort($arr2);
print_r($arr2);

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!