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.