FunctionRacerProblemsRace

Relative Sort Array

EasySorting and searchingPython, JavaScript, TypeScript, Java, C++

The items of arr2 are all different, and every one of them appears in arr1.

Return arr1 rearranged so that the values found in arr2 come first, in the order they appear in arr2 (with all their repeats), followed by the remaining values in increasing order.

Example 1

arr1 [2,3,1,3,2,4,6,7,9,2,19]
arr2 [2,1,4,3,9,6]
returns [2,2,2,1,4,3,3,9,6,7,19]

The 2s, then 1, 4, 3s, 9, 6, then the leftovers 7 and 19.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Sorting and searching problems