Write a java program to sort elements in descending order according to their frequency. If the two elements have same frequency then output them in the order of their input.

Example:

Store the input into List and HaspMap. Sort the list according to the count stored in the HashMap. For this, we need to implement the comparator Interface.

Override the compare method, return 0 if the frequency (i.e count) of the two number is same otherwise return the frequency difference.

Recommended: Comparator Interface in Java

Output

Leave a Reply

Close Menu