-
Notifications
You must be signed in to change notification settings - Fork 1
/
1122.js
47 lines (38 loc) · 1006 Bytes
/
1122.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Relative Sort Array [2023 solution]
/**
* @param {number[]} arr1
* @param {number[]} arr2
* @return {number[]}
*/
var relativeSortArray = function (arr1, arr2) {
var output = [];
while (arr2.length) {
var curr = arr2.shift();
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] === curr) {
output.push(arr1.splice(i, 1))
i--;
}
}
}
return [...output, ...arr1.sort((a, b) => a - b)]
};
// Relative Sort Array [2024 solution]
/**
* @param {number[]} arr1
* @param {number[]} arr2
* @return {number[]}
*/
var relativeSortArray = function (arr1, arr2) {
let sortedArr = [];
for (let num of arr2) {
for (let i = 0; i < arr1.length; i++) {
if (num === arr1[i]) {
let currNum = arr1.splice(i, 1);
sortedArr.push(currNum[0]);
i--;
}
}
}
return [...sortedArr, ...arr1.sort((a, b) => a - b)];
};