Find the letter with the most repeated occurrences. If there are multiple duplicates, obtain them.
A string may contain ~ Multiple characters in z, such as String = "aavlasdjflajeeeeewjjowejjojasjfesdvoeawje";. Find the letter with the most occurrences and the number of occurrences. If there are multiple duplicates, obtain them.
1. Introduce TreeSet: quickly find all the strings that appear through the set
2. Introduction of ArrayList: In order to quickly sort, the sorted string is generated using StringBuffer.
3. Use the basic method indexOfLaseIndexOf in the String api to calculate the maximum value of each String in the TreeSet.
4. If the same result appears, all the same records will be recorded in a list.
5. Record the first character that appears most frequently
6. Calculate which of the largest strings are the most frequently displayed.
package cn.usst.DataTest2;import java.util.ArrayList;import java.util.Collections;import java.util.Iterator;import java.util.TreeSet;public class DataTest {public static void main(String[] args) {String input = "aavlasdjflajeeeeewjjowejjojasjfesdvoeawje";new DataTest().doString(input);}@SuppressWarnings({ "rawtypes", "unchecked" })private void doString(String input) {char[] chars = input.toCharArray();ArrayList lists = new ArrayList();TreeSet set = new TreeSet();for (int i = 0; i < chars.length; i++){lists.add(String.valueOf(chars[i]));set.add(String.valueOf(chars[i]));}System.out.println(set);Collections.sort(lists);System.out.println(lists);StringBuffer sb = new StringBuffer();for ( int i = 0; i < lists.size(); i++){sb.append(lists.get(i));}input = sb.toString();System.out.println(input);int max = 0; String maxString = "";ArrayList maxlist = new ArrayList();Iterator its = set.iterator();while(its.hasNext()){String os = (String) its.next();int begin = input.indexOf(os);int end = input.lastIndexOf(os);int value = end - begin + 1;if(value > max){max = value; maxString = os; maxlist.add(os);}else if(value == max){maxlist.add(os);}}int index = 0;for (int i = 0; i< maxlist.size(); i++){if(maxlist.get(i).equals(maxString)){index = i; break;}}System.out.println();for(int i = index; i < maxlist.size(); i++){System.out.println("maxdata = " + maxlist.get(i) );}System.out.println("max = " + max);}}