Given an array of integers, sort the elements based on the maximum frequency of any digit within each number in descending order. In case of a tie, sort the numbers in ascending order.
Explanation: The frequency of digits in 113 is 2 (1 appears twice), in 112 is 2 (1 appears twice), in 66 is 2 (6 appears twice), in 55 is 2 (5 appears twice), in 234 is 1 (each digit appears once), in 89 is 1 (each digit appears once) and in 7 is 1 (7 appears once). In descending order of frequency and ascending order of numbers, the sorted array is [113, 112, 66, 55, 234, 89, 7].
Explanation: The frequency of digits in 999 is 3 (9 appears thrice), in 101 is 2 (1 appears twice), in 42 is 1 (each digit appears once), in 17 is 1 (each digit appears once) and in 8 is 1 (8 appears once). In descending order of frequency and ascending order of numbers, the sorted array is [999, 101, 42, 17, 8].
Non-comparison based sorting
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak