Find the letter with the most repeated occurrences. If there are multiple duplicates, obtain them.

Source: Internet
Author: User

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


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.