Input: arr1[] = [2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8]
arr2[] = [2, 1, 8, 3]
Output: [2, 2, 1, 1, 8, 8, 3, 5, 6, 7, 9]
Explanation: Elements in arr1[] that are also present in arr2[] appear first, following the exact order given in arr2[]. The remaining elements, which are not in arr2[], are sorted in ascending order and placed at the end of the array.
Input: arr1[] = [4, 5, 1, 1, 3, 2]
arr2[] = [3, 1]
Output: [3, 1, 1, 2, 4, 5]
Explanation: Elements follow the order of arr2[], others are sorted and placed at the end.
The main idea is to use a hash map to count frequencies of elements in arr1, so we can efficiently place elements in the desired order. Then, iterate through arr2 to append matching elements in order, followed by the sorted remaining elements from arr1. This ensures correct relative order from arr2, and sorted placement of extras.