Public static void main (string [] ARGs ){
/**
* I am doing this and saving it using map. When the string is large, my efficiency is high.
*/
String test = "asdqwasdasdasdi ";
// Count the execution time
Long time1 = system. currenttimemillis ();
Char [] DATA = test. tochararray ();
Map <string, integer> map = new hashmap <string, integer> ();
For (INT I = 0; I <data. length; I ++)
{
Char [] temp = {data [I]};
String key = new string (temp );
// First check whether the character exists in the map. If the value is added to 1, the character is not saved, and the number of occurrences is set to 1.
If (null = map. Get (key ))
{
Map. Put (Key, 1 );
} Else {
Int value = integer. valueof (Map. Get (key ));
Map. Remove (key );
Map. Put (Key, integer. valueof (Value + 1 ));
}
}
// Output the key and value of the map
Int max = 0; // count the maximum number of occurrences
String maxkey = ""; // count the maximum number of occurrences
Set set = map. keyset ();
Iterator it = set. iterator ();
While (it. hasnext ())
{
String key = (string) it. Next ();
// System. Out. println ("key" + key + "value" + map. Get (key ));
If (max <integer. valueof (Map. Get (key ))){
Max = integer. valueof (Map. Get (key ));
Maxkey = key;
}
}
System. Out. println ("------------------");
System. Out. println ("the most frequently occurring character is" + maxkey + ", and the number of occurrences is" + MAX + "");
Long time2 = system. currenttimemillis ();
/**
* The code is concise but inefficient.
*/
String STR = test;
Int Len = Str. Length ();
Char [] TMP = Str. tochararray ();
Int maxcount = 0;
Char maxchar = 0;
Int tempcount;
For (INT I = 0; I <Len; I ++ ){
Tempcount = 0;
For (Int J = 0; j <Len; j ++ ){
If (TMP [J] = TMP [I]) tempcount ++;
}
// Isletter determines whether the specified character is a letter.
If (tempcount> maxcount & character. isletter (TMP [I]) {
Maxcount = tempcount;
Maxchar = TMP [I];
}
}
System. Out. println (maxchar );
System. Out. println (maxcount );
Long time3 = system. currenttimemillis ();
System. Out. println ();
System. Out. println ("the first method consumes time" + (time2-time1 ));
System. Out. println ("the second method consumes time" + (time3-time2 ));
}