Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.

Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that do not appear in arr2 should be placed at the end of arr1 in ascending order.

&nbsp;
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]


&nbsp;
Constraints:


	1 &lt;= arr1.length, arr2.length &lt;= 1000
	0 &lt;= arr1[i], arr2[i] &lt;= 1000
	All the elements of arr2 are distinct.
	Each&nbsp;arr2[i] is in arr1.


class Solution:
    def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
        arr = []
        hashmap = {char: arr1.count(char) for char in arr1}
        [arr.extend([char] * hashmap.pop(char)) for char in arr2]
        return arr + sorted([int(key) * hashmap[key] for key in hashmap]
class Solution {
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        Map <Integer, Integer> map = new TreeMap();
        for(int i = 0; i<arr1.length; i++){
            if(map.containsKey(arr1[i])){
                map.replace(arr1[i], map.get(arr1[i]),map.get(arr1[i])+1);
            }
            else{
                map.put(arr1[i], 1);
            }
        }

        int[] arr = new int [arr1.length];
        int ind = 0;
        for(int i = 0; i<arr2.length; i++){

            for(int j = 0; j<map.get(arr2[i]);j++){
                arr[ind] = arr2[i];
                ind++;
            }
            map.remove(arr2[i]);
        }

        for(int i: map.keySet()){

            for(int j = 0; j<map.get(i);j++){
                arr[ind] = i;
                ind++;
            }

        }

        return arr;
    }
}
class Solution {
public:
    vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
        map<int,int>sk;
        vector<int>res;
        for(auto i:arr1){
            sk[i]++;
        }
        for(auto j:arr2){
            for(auto z:sk){
                if(z.first==j){
                    int x=z.second;
                    for(int l=0;l<x;l++){
                        res.push_back(z.first);
                    }
                }
               
            }
        }
        for(auto z:sk){
            if(z.second==4 && find(res.begin(), res.end(),z.first)==res.end()){
                   int p=z.second;
                  for(int aqq=0;aqq<p;aqq++){
                      res.push_back(z.first);
                  }
                }
            
            if(z.second==3 && find(res.begin(), res.end(),z.first)==res.end()){
                   int p=z.second;
                  for(int aq=0;aq<p;aq++){
                      res.push_back(z.first);
                  }
                }
            
              if(z.second==2 && find(res.begin(), res.end(),z.first)==res.end()){
                   int p=z.second;
                  for(int a=0;a<p;a++){
                      res.push_back(z.first);
                  }
                }
            
         if(z.second==1 && find(res.begin(), res.end(),z.first)==res.end()){
                    res.push_back(z.first);
                }
        }
        
        return res;
            
    }
};
/**
 * @param {number[]} arr1
 * @param {number[]} arr2
 * @return {number[]}
 */
var relativeSortArray = function(arr1, arr2) {
  const indices = new Map();
  arr2.forEach(indices.set, indices);

  return arr1.sort((a, b) => {
    if (indices.has(a)) {
      return indices.has(b) ? indices.get(a) - indices.get(b) : -1;
    }
    if (indices.has(b)) {
      return 1;
    }
    return a - b;
  });
};
Relative Sort Array