给定一个完整数组,如何识别不完整数组中缺少哪些元素?
How to identify which elements are missing from an incomplete array, given a complete array?
我正在尝试为这个问题编写一个实用的解决方案。我已经有一个使用循环和突变而不使用递归的解决方案(我知道我在下面尝试的涉及突变,我正在努力避免这种情况)。
请在下面的评论中查看预期输出。需要强调的是,元素不一定是唯一的,排序也无所谓。
const getNamesWhichFillHoles = (namesArrayWithHoles, namesArrayFilled, fillers = []) => {
const holey = [...namesArrayWithHoles].filter(Boolean);
const filled = [...namesArrayFilled].filter(Boolean);
const fillerIndexInFilled = filled.findIndex(name => !holey.includes(name));
if (fillerIndexInFilled === -1) return fillers;
const filler = filled[fillerIndexInFilled];
const fillerIndexInHoley = holey.findIndex(name => name == filler);
fillers.push(filler);
filled[fillerIndexInFilled] = null;
holey[fillerIndexInHoley] = null;
return getNamesWhichFillHoles(holey, filled, fillers);
}
const namesArrayWithHoles = ['Bob', null, null, 'Sue', null];
const namesArrayFilled = ['Jim', 'Bob', 'Bob', 'Sam', 'Sue',];
const fillerNames = getNamesWhichFillHoles(namesArrayWithHoles, namesArrayFilled);
console.log(fillerNames); // [ 'Jim', 'Sam' ]
// should be: [ 'Jim', 'Sam', 'Bob' ]
您可以按照以下方式进行操作:
const namesArrayWithHoles = ['Bob', null, null, 'Sue', null];
const namesArrayFilled = ['Jim', 'Bob', 'Bob', 'Sam', 'Sue'];
const result = namesArrayFilled.reduce(
(acc, elem) => {
const index = acc[0].indexOf(elem);
if (index != -1) {
return [acc[0].slice(0, index).concat(acc[0].slice(index + 1)), acc[1]];
}
return [acc[0], acc[1].concat(elem)];
},
[namesArrayWithHoles, []]
)[1];
console.log(result);
我找到了这个解决方案。它确实假定没有 null
值,因为那只是一种干扰。
const getMissingValues = (partial, complete) => {
const equals = a => b => b == a;
const push = (arr, val, count) => count ? push([...arr, val], val, count - 1) : [...arr];
return [...new Set(complete)].reduce((missing, val) => {
const { length: a } = partial.filter(equals(val));
const { length: b } = complete.filter(equals(val));
const diff = (b - a);
return push(missing, val, diff);
}, []);
}
const partial = ['Bob', 'Sue'];
const complete = ['Jim', 'Bob', 'Bob', 'Bob', 'Sue', 'Sue', 'Sam'];
const missing = getMissingValues(partial, complete);
console.log(missing); // [ 'Jim', 'Bob', 'Bob', 'Sue', 'Sam' ]
我正在尝试为这个问题编写一个实用的解决方案。我已经有一个使用循环和突变而不使用递归的解决方案(我知道我在下面尝试的涉及突变,我正在努力避免这种情况)。
请在下面的评论中查看预期输出。需要强调的是,元素不一定是唯一的,排序也无所谓。
const getNamesWhichFillHoles = (namesArrayWithHoles, namesArrayFilled, fillers = []) => {
const holey = [...namesArrayWithHoles].filter(Boolean);
const filled = [...namesArrayFilled].filter(Boolean);
const fillerIndexInFilled = filled.findIndex(name => !holey.includes(name));
if (fillerIndexInFilled === -1) return fillers;
const filler = filled[fillerIndexInFilled];
const fillerIndexInHoley = holey.findIndex(name => name == filler);
fillers.push(filler);
filled[fillerIndexInFilled] = null;
holey[fillerIndexInHoley] = null;
return getNamesWhichFillHoles(holey, filled, fillers);
}
const namesArrayWithHoles = ['Bob', null, null, 'Sue', null];
const namesArrayFilled = ['Jim', 'Bob', 'Bob', 'Sam', 'Sue',];
const fillerNames = getNamesWhichFillHoles(namesArrayWithHoles, namesArrayFilled);
console.log(fillerNames); // [ 'Jim', 'Sam' ]
// should be: [ 'Jim', 'Sam', 'Bob' ]
您可以按照以下方式进行操作:
const namesArrayWithHoles = ['Bob', null, null, 'Sue', null];
const namesArrayFilled = ['Jim', 'Bob', 'Bob', 'Sam', 'Sue'];
const result = namesArrayFilled.reduce(
(acc, elem) => {
const index = acc[0].indexOf(elem);
if (index != -1) {
return [acc[0].slice(0, index).concat(acc[0].slice(index + 1)), acc[1]];
}
return [acc[0], acc[1].concat(elem)];
},
[namesArrayWithHoles, []]
)[1];
console.log(result);
我找到了这个解决方案。它确实假定没有 null
值,因为那只是一种干扰。
const getMissingValues = (partial, complete) => {
const equals = a => b => b == a;
const push = (arr, val, count) => count ? push([...arr, val], val, count - 1) : [...arr];
return [...new Set(complete)].reduce((missing, val) => {
const { length: a } = partial.filter(equals(val));
const { length: b } = complete.filter(equals(val));
const diff = (b - a);
return push(missing, val, diff);
}, []);
}
const partial = ['Bob', 'Sue'];
const complete = ['Jim', 'Bob', 'Bob', 'Bob', 'Sue', 'Sue', 'Sam'];
const missing = getMissingValues(partial, complete);
console.log(missing); // [ 'Jim', 'Bob', 'Bob', 'Sue', 'Sam' ]