Give you a string that contains space and other punctuation marks and you need to calculate the appearance

Source: Internet
Author: User
Tags set set

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 ));

}
 

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.