Java finds the string with the most occurrences in the string and the number of occurrences
Interview question of Jinshan company: a string may contain multiple characters in a ~ Z. if there is repetition, such as string data = "aavzcadfdsfdhshgwasdfasdf", find the letter and number of occurrences most frequently. If there are multiple repetitions, find out all.
The solution to this problem is as follows:
Introducing TreeSet: quickly find all existing strings through the collection. Introducing ArrayList: for quick sorting, generate the sorted strings through StringBuffer. Calculate the maximum number of occurrences of each string through the indexof method and lastIndexOf method of string. Use HashMap to store the multiple strings and times
The code is as follows:
The operation results are as follows:
The most repeated letter is: n the most repeated times is: 4
When strings are repeated the same number of times, they can also be printed.
as
The operation results are as follows:
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.