BackhardSorting

Ordinal Digit Sorting Solution

Problem Statement

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.

Example 1
Input
[113, 234, 55, 7, 66, 89, 112]
Output
[55,66,112,113,7,89,234]

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].

Example 2
Input
[101, 42, 17, 8, 999]
Output
[999,101,8,17,42]

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].

Constraints

  • 1 <= arr.length <= 10^5
  • 1 <= arr[i] <= 10^6
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free