Code Logo

Catalog Shelf Lineup

Published at05 Jan 2026
Easy 7 views
Like5

You are given an array of catalog numbers and need to return them in ascending order.

Nothing is removed along the way. Negative values stay, duplicate values stay, and if the list is empty the answer is just an empty list. The only change is the arrangement.

For example, nums = [6,-1,6,2] becomes [-1,2,6,6]. Both 6s are still there, just moved into the correct position. If nums = [3,1,4,1], the result is [1,1,3,4].

So the task is simply to line up the catalog values from smallest to largest.

Example Input & Output

Example 1
Input
nums = [6,-1,6,2]
Output
[-1,2,6,6]
Explanation

Negative placeholders and repeats remain visible in ascending order.

Example 2
Input
nums = [0]
Output
[0]
Explanation

A single sample remains unchanged because it is already in order.

Example 3
Input
nums = [3,1,4,1]
Output
[1,1,3,4]
Explanation

Matching labels appear together after sorting while the smallest value leads the list.

Algorithm Flow

Recommendation Algorithm Flow for Catalog Shelf Lineup
Recommendation Algorithm Flow for Catalog Shelf Lineup

Solution Approach

This is a plain sorting problem. Any correct ascending sort works.

You can make a copy of the array and sort it numerically, or sort the array in place if that is allowed. The important detail is to compare numbers by value, not as strings.

Because duplicates and negative values are part of the input, the sort should keep every element exactly once and only change the order.

A comparison sort runs in O(n log n) time. Extra space depends on the language and sorting method you use.

Best Answers

java
import java.util.*;

class Solution {
    public int[] catalog_shelf_lineup(int[] nums) {
        int[] result = nums.clone();
        Arrays.sort(result);
        return result;
    }
}