#1122

Relative Sort Array

newbie · 230 · lc easy +17 · verified · 75.1% accepted · 3,348 likes · top 86%

Description

Given arrays arr1 and arr2 where arr2's elements are distinct and all appear in arr1, sort arr1 so that elements appearing in arr2 come first in the same relative order as arr2. Elements not in arr2 follow in ascending order.

Example 1:

Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
Output: [2,2,2,1,4,3,3,9,6,7,19]

Example 2:

Input: arr1 = [28,6,22,8,44,17], arr2 = [22,28,8,6]
Output: [22,28,8,6,17,44]

Code

1
2
3