* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
4.6 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
a5de63ebea8dbee56860f4f2 | 数组的对称差 | 5 | 16008 | diff-two-arrays |
--description--
在这道题目中,我们需要实现一个函数,它可以比较两个输入数组并返回一个新数组;返回的新数组需包含传入的两个数组中,仅在一个数组里出现的元素。如果某个元素同时出现在两个数组中,则不应包含在返回的数组里。换言之,我们需要返回两个数组的对称差。
注意:
返回数组中的元素顺序不会影响挑战是否通过。
--hints--
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])
应返回一个数组。
assert(typeof diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]) === 'object');
["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]
应返回 ["pink wool"]
。
assert.sameMembers(
diffArray(
['diorite', 'andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],
['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']
),
['pink wool']
);
["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]
应返回一个长度为 1 的数组。
assert(
diffArray(
['diorite', 'andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],
['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']
).length === 1
);
["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]
应返回 ["diorite", "pink wool"]
。
assert.sameMembers(
diffArray(
['andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],
['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']
),
['diorite', 'pink wool']
);
["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]
应返回一个长度为 2 的数组。
assert(
diffArray(
['andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],
['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']
).length === 2
);
["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]
应返回 []
。
assert.sameMembers(
diffArray(
['andesite', 'grass', 'dirt', 'dead shrub'],
['andesite', 'grass', 'dirt', 'dead shrub']
),
[]
);
["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]
应返回一个空数组。
assert(
diffArray(
['andesite', 'grass', 'dirt', 'dead shrub'],
['andesite', 'grass', 'dirt', 'dead shrub']
).length === 0
);
[1, 2, 3, 5], [1, 2, 3, 4, 5]
应返回 [4]
。
assert.sameMembers(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]), [4]);
[1, 2, 3, 5], [1, 2, 3, 4, 5]
应返回一个长度为 1 的数组。
assert(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]).length === 1);
[1, "calf", 3, "piglet"], [1, "calf", 3, 4]
应返回 ["piglet", 4]
。
assert.sameMembers(diffArray([1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]), [
'piglet',
4
]);
[1, "calf", 3, "piglet"], [1, "calf", 3, 4]
应返回一个长度为 2 的数组。
assert(diffArray([1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]).length === 2);
[], ["snuffleupagus", "cookie monster", "elmo"]
应返回 ["snuffleupagus", "cookie monster", "elmo"]
。
assert.sameMembers(diffArray([], ['snuffleupagus', 'cookie monster', 'elmo']), [
'snuffleupagus',
'cookie monster',
'elmo'
]);
[], ["snuffleupagus", "cookie monster", "elmo"]
应返回一个长度为 3 的数组。
assert(diffArray([], ['snuffleupagus', 'cookie monster', 'elmo']).length === 3);
[1, "calf", 3, "piglet"], [7, "filly"]
应返回 [1, "calf", 3, "piglet", 7, "filly"]
。
assert.sameMembers(diffArray([1, 'calf', 3, 'piglet'], [7, 'filly']), [
1,
'calf',
3,
'piglet',
7,
'filly'
]);
[1, "calf", 3, "piglet"], [7, "filly"]
应返回一个长度为 6 的数组。
assert(diffArray([1, 'calf', 3, 'piglet'], [7, 'filly']).length === 6);
--seed--
--seed-contents--
function diffArray(arr1, arr2) {
var newArr = [];
return newArr;
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
--solutions--
function diffArray(arr1, arr2) {
var newArr = [];
var h1 = Object.create(null);
arr1.forEach(function(e) {
h1[e] = e;
});
var h2 = Object.create(null);
arr2.forEach(function(e) {
h2[e] = e;
});
Object.keys(h1).forEach(function(e) {
if (!(e in h2)) newArr.push(h1[e]);
});
Object.keys(h2).forEach(function(e) {
if (!(e in h1)) newArr.push(h2[e]);
});
return newArr;
}