Skip to main content
LeetCode 1122, Easy. Topics: Array, Hash Table, Counting Sort, Sorting. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 14 parametrized pytest cases, and a playground notebook:

Problem

Given two arrays <code>arr1</code> and <code>arr2</code>, the elements of <code>arr2</code> are distinct, and all elements in <code>arr2</code> are also in <code>arr1</code>. Sort the elements of <code>arr1</code> such that the relative ordering of items in <code>arr1</code> are the same as in <code>arr2</code>. Elements that do not appear in <code>arr2</code> should be placed at the end of <code>arr1</code> in <strong>ascending</strong> order.

Examples

Constraints

  • <code>1 <= arr1.length, arr2.length <= 1000</code>
  • <code>0 <= arr1[i], arr2[i] <= 1000</code>
  • All the elements of <code>arr2</code> are <strong>distinct</strong>.
  • Each <code>arr2[i]</code> is in <code>arr1</code>.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026