BackeasySorting

Least Significant Digit Frequency Sort 2 Solution

Problem Statement

Given an array of non-negative integers, sort the array based on the frequency of the least significant digit of each number in ascending order, and then by the least significant digit itself in ascending order. If two or more numbers have the same least significant digit and frequency, their original order is preserved.

Example 1
Input
[13, 22, 7, 4, 12]
Output
[13,4,7,22,12]

Explanation: The frequency of the least significant digit for each number is as follows: 3 (1 time), 2 (2 times), 7 (1 time), 4 (1 time), 2 (2 times). Sorting based on this frequency and then by the digit gives the output.

Example 2
Input
[111, 222, 333, 444, 555]
Output
[111, 222, 333, 444, 555]

Explanation: The frequency of the least significant digit for each number is the same (1 time), so the output is sorted by the least significant digit itself in ascending order.

Constraints

  • 1 <= length of the array <= 10^5
  • 0 <= each integer in the array <= 10^9
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