Original question
A collection of n elements with the same elements.
A new collection that needs to be sorted by the number of repeating elements.
Enter {"A", "B", "C", "C", "A", "C"}
Output {"C", "A", "B"}
Seeking algorithm
Import Java.util.arraylist;import java.util.collections;import java.util.comparator;import java.util.HashMap; Import Java.util.list;import Java.util.map;import Java.util.map.entry;public class Arrsort {public static void main ( String[] args) {string[] Strarr = {"A", "B", "C", "C", "A", "C"}; System.out.println (ArraySort (Strarr));} public static String ArraySort (string[] arr) {//define map, hold characters in array and occurrences map<string, integer> map = new Hashmap<strin G, integer> (); Iterates through the array, storing characters and occurrences in map for (String Str:arr) {Integer count = map.get (str); if (null! = count) {map.put (str, count + 1); } else {map.put (str, 1); }}//define list, store Entry list<map.entry<string in Map, integer>> list = new arraylist& Lt Map.entry<string, integer>> (); List.addall (Map.entryset ()); For entry in list, sort by value values in descending order Collections.sort (list, new comparator<entrY<string, integer>> () {public int compare (entry<string, integer> arg0, entry<string, integer& Gt ARG1) {return Arg1.getvalue (). CompareTo (Arg0.getvalue ()); } }); Defines the StringBuffer, storing the returned string StringBuffer retstr = new StringBuffer (); For (entry<string, integer> entry:list) {retstr.append (Entry.getkey ()). Append (","); }//Assemble the string into the desired format return "{" + Retstr.delete (Retstr.length ()-1, Retstr.length ()). ToString () + "}"; }}
Sorts the characters in the array by the number of occurrences of the output