Ctrl K

Sort by a Reference Order

Easy
A warehouse has boxes labeled with product codes items that need to go on a shelf. Some codes have a priority, given as an array order with no repeats, and every code in order appears in items at least once. Arrange items as follows: - first come the codes from order, in the order they are listed in order (equal codes stand together); - then all codes that are not in order, in increasing order. Return the resulting array.
Example 1
items=[5, 1, 7, 5, 3, 0, 1, 9], order=[7, 1, 5] → [7, 1, 1, 5, 5, 0, 3, 9]

All 7s come first, then all 1s, then the 5s. The values 0, 3 and 9 are not in order, so they go last in increasing order.

Example 2
items=[4, 2, 4], order=[2, 4] → [2, 4, 4]
Constraints
  • 1 ≤ len(items), len(order) ≤ 1000
  • 0 ≤ items[i], order[i] ≤ 1000
  • All values in order are distinct
  • Every value in order appears in items
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsHow Many Are Smaller EasyThe Candidate With Most Votes EasyDoes Any Value Repeat EasyThe Lost Ticket EasyValues in Both Lists Easy
loading Python…
Input
items=[5, 1, 7, 5, 3, 0, 1, 9], order=[7, 1, 5]
Expected
[7, 1, 1, 5, 5, 0, 3, 9]

Run checks the first 3 tests in your browser. Submit checks all 129 tests on the server, hidden ones included.