Count the number of letters in the string that have the most occurrences, and if there are multiple duplicates, find out

Source: Internet
Author: User

Package Javastudy;import Java.util.arraylist;import Java.util.collections;import java.util.iterator;import Java.util.treeset;public class Test1 {public static void main (string [] args) {string str = "Aacdggdhrdhgsjrsovoiwimtcpiq" ; New Test1 (). dostring (str);} public void dostring (String str) {char [] chars = Str.tochararray (); arraylist<string> lists = new arraylist<string> (); treeset<string> set = new treeset<string> (); for (int i=0;i<chars.length;i++) {Lists.add (String.valueOf (Chars[i])); Set.add (string.valueof (Chars[i));} Order and no repetition, find all occurrences of System.out.println ("set:" +set);       Order can have repetition, ArrayList can quickly sort collections.sort (lists); System.out.println ("lists:" +lists);//Gets the sorted string StringBuffer sb = new StringBuffer (); for (int i=0;i<lists.size (); i+ +) {sb.append (Lists.get (i));} str = sb.tostring (); System.out.println ("str after sorted:" +STR);//Statistics The first occurrence of the most occurrences of char, noting that the maximum number of occurrences is Max, and the corresponding char is maxstringint max = 0; String maxstring = ""; arraylist<string> maxlist = new ArraylIst<string> (); Iterator ITR = Set.iterator (); while (Itr.hasnext ()) {string os= (String) itr.next (); int begin = Str.i Ndexof (OS), int end = Str.lastindexof (OS), int value = End-begin+1;if (Value>max) {max= value; maxstring = OS; maxlist.a DD (OS);} else if (value = = max) {maxlist.add (OS);}} Find the largest position int index = 0;for (int i=0;i<maxlist.size (); i++) {if (Maxlist.get (i). Equals (maxstring)) {index =i;break;}} SYSTEM.OUT.PRINTLN ("Max Data"), for (int i=index;i<maxlist.size (); i++) {System.out.println (Maxlist.get (i) + "");} System.out.println (); System.out.println ("Max:" + max);}}

The output result is

Set:[a, C, D, G, H, I, J, M, O, p, Q, R, S, T, V, W]
Lists:[a, A, C, C, D, D, D, G, G, G, H, H, I, I, I, J, M, O, O, p, Q, R, R, s, S, T, V, W]
STR after SORTED:AACCDDDGGGHHIIIJMOOPQRRSSTVW
Max data
D
G
I

  

Count the number of letters in the string that have the most occurrences, and if there are multiple duplicates, find out

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.