Php interview question: how do I know which character of an unknown string has the most occurrences? (A string may consist of letters, numbers, and other characters) please write the code .. Php interview question: how do I know which character of an unknown string has the most occurrences? (A string may consist of letters, numbers, and other characters) please write the code. How do I know a php interview question: how do I know which character of an unknown string has the most occurrences? (A string may consist of letters, numbers, and other characters) please write the code.
How do I know which character of an unknown string has the most occurrences? (A string may consist of letters, numbers, and other characters) please write the code.
$ Str = "asdfgfdas323344 ##\ \ $ fdsdfg * $ ** $ 443563536254fas"; // any length string
// Solution 1 (the fastest solution, but the basic skills must be solid)
$ Arr = str_split ($ str );
$ Arr = array_count_values ($ arr );
Arsort ($ arr );
Print_r ($ arr );
// Solution 2 (required for logical capability)
$ Arr = str_split ($ str );
$ Con = array ();
Foreach ($ arr as $ v ){
If (! @ $ Con [$ v]) {
@ $ Con [$ v] = 1;
} Else {
@ $ Con [$ v] ++;
}
}
Arsort ($ con );
Print_r ($ con );
// Solution 3
$ Arr = str_split ($ str );
$ Unique = array_unique ($ arr );
Foreach ($ unique as $ ){
$ Arr2 [$ a] = substr_count ($ str, $ );
}
Arsort ($ arr2 );
Print_r ($ arr2 );
Why? (A string may consist of letters, numbers, and other characters) please write the code. How to know...