IZUM,
вариант для любого количества начальных массивов (все элементы разные)
function repeat(arr) {
let retry = [],
temp;
arr.reduce(
function(prev, current, index, arr) {
if (prev === current) temp.push(index);
else temp = [index];
if (temp.length == 2) retry.push(temp);
return current;
}
);
return retry
}
const ms = [2, 8, 13, 15, 19, 27, 33, 36, 43, 46],
mo = [0, 4, 6, 11, 17, 21, 25, 31, 38, 41, 45, 48],
mt = [3, 22, 23, 24];
function merge(...arg) {
let l;
if( typeof(arg.at(-1)) == "number") l = arg.pop();
let s = arg.flat();
s.sort((a, b) => a - b);
let arr = s.map(k => arg.findIndex(a => a.includes(k)));
arr = repeat(arr).map(ar => ar.map(i => s[i]));
if (l !== void(0)) {
let i = arr.findIndex(a => a[0] > l);
let dp = arr[i] || [],
dL = [];
if (i > 0)
for (; i--;) {
let b = arr[i].at(-1);
if (b < l) {
dL = arr[i]; break;
}
}
return [dL, dp]
}
return arr
}
console.log(merge(ms, mo));
console.log(merge(ms, mo, mt));
console.log(merge(ms, mo, mt, 17));
console.log(merge(ms, mo, mt, 25));