2.0 KiB
2.0 KiB
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
a39963a4c10bc8b4d4f06d7e | 过滤数组元素 | 5 | 16046 | seek-and-destroy |
--description--
你将获得一个初始数组(destroyer
函数中的第一个参数),后跟一个或多个参数。 从初始数组中移除所有与后续参数相等的元素。
注意: 你可以使用 arguments
对象。
--hints--
destroyer([1, 2, 3, 1, 2, 3], 2, 3)
应返回 [1, 1]
。
assert.deepEqual(destroyer([1, 2, 3, 1, 2, 3], 2, 3), [1, 1]);
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3)
应返回 [1, 5, 1]
。
assert.deepEqual(destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3), [1, 5, 1]);
destroyer([3, 5, 1, 2, 2], 2, 3, 5)
应返回 [1]
。
assert.deepEqual(destroyer([3, 5, 1, 2, 2], 2, 3, 5), [1]);
destroyer([2, 3, 2, 3], 2, 3)
应返回 []
。
assert.deepEqual(destroyer([2, 3, 2, 3], 2, 3), []);
destroyer(["tree", "hamburger", 53], "tree", 53)
应返回 ["hamburger"]
。
assert.deepEqual(destroyer(['tree', 'hamburger', 53], 'tree', 53), [
'hamburger'
]);
destroyer(["possum", "trollo", 12, "safari", "hotdog", 92, 65, "grandma", "bugati", "trojan", "yacht"], "yacht", "possum", "trollo", "safari", "hotdog", "grandma", "bugati", "trojan")
应返回 [12,92,65]
。
assert.deepEqual(
destroyer(
[
'possum',
'trollo',
12,
'safari',
'hotdog',
92,
65,
'grandma',
'bugati',
'trojan',
'yacht'
],
'yacht',
'possum',
'trollo',
'safari',
'hotdog',
'grandma',
'bugati',
'trojan'
),
[12, 92, 65]
);
--seed--
--seed-contents--
function destroyer(arr) {
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
--solutions--
function destroyer(arr) {
var hash = Object.create(null);
[].slice.call(arguments, 1).forEach(function(e) {
hash[e] = true;
});
return arr.filter(function(e) { return !(e in hash);});
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);