-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefrag.js
26 lines (21 loc) · 979 Bytes
/
defrag.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
const defrag = (arr, orderArr) => {
const state = {};
return arr.reduce((acc, item, index) => {
state[item] = +(state[item] === undefined) + (state[item]+1 || 0);
let position = 0;
if (orderArr.findIndex((ordItem) => {
position += state[ordItem] || 0;
return ordItem === item;
}) >= 0) {
acc.splice(position-1, 0, item);
}
else {
acc.push(item);
}
return acc;
}, []);
}
console.log("defrag([3,4,5,3,3,2,1,3,5,34,6,7,3,5,21,2,3,2,1], [2, 3, 1]) => ", defrag([3,4,5,3,3,2,1,3,5,34,6,7,3,5,21,2,3,2,1], [2, 3, 1]));
console.log("defrag([3,4,5,3,3,2,1,3,5,34,6,7,3,5,21,2,3,2,1], [2, 3, 1, 5]) => ", defrag([3,4,5,3,3,2,1,3,5,34,6,7,3,5,21,2,3,2,1], [2, 3, 1, 5]));
console.log("defrag([3,4,5,3,3,2,1,3,5,34,6,7,3,5,21,2,3,2,1], [5, 2, 3, 1]) => ", defrag([3,4,5,3,3,2,1,3,5,34,6,7,3,5,21,2,3,2,1], [5, 2, 3, 1]));
console.log("defrag([3,4,5,3,3,2,1,3,5,34,6,7,3,5,21,2,3,2,1], []) => ", defrag([3,4,5,3,3,2,1,3,5,34,6,7,3,5,21,2,3,2,1], []));