freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-an.../intermediate-algorithm-scri.../diff-two-arrays.md

191 lines
4.4 KiB
Markdown
Raw Normal View History

---
id: a5de63ebea8dbee56860f4f2
title: Diff Two Arrays
challengeType: 5
forumTopicId: 16008
dashedName: diff-two-arrays
---
# --description--
Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
chore(learn): audit javascript algorithms and data structures (#41092) * chore(learn): audit basic algorithm scripting * chore(learn): audit basic data structures * chore(learn): audit basic javascript * chore(learn): audit debugging * chore(learn): audit es6 * chore(learn): audit functional programming * chore(learn): audit intermidate algorithms * chore(learn): audit js projects * chore(learn): audit object oriented programming * chore(learn): audit regex * fix(learn): remove stray . * fix(learn): string to code * fix(learn): missed some * fix(learn): clarify strings Based on Randy's feedback, clarifies string instances where quotes were removed in favour of back ticks. * fix: apply suggestions - thanks Randy! :) Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: non-suggestion comments * chore(learn): remove comments from codes Removes the comments from the description and instruction code blocks to ensure that all relevant information is translatable. * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: revert crowdin fix * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * chore: change voice * fix: Christopher Nolan * fix: expressions would evaluate * fix: will -> would * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: to work to push * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2021-03-03 00:12:12 +00:00
**Note:** You can return the array with its elements in any order.
# --hints--
`diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])` should return an array.
```js
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"]` should return `["pink wool"]`.
```js
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"]` should return an array with one item.
```js
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"]` should return `["diorite", "pink wool"]`.
```js
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"]` should return an array with two items.
```js
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"]` should return `[]`.
```js
assert.sameMembers(
diffArray(
['andesite', 'grass', 'dirt', 'dead shrub'],
['andesite', 'grass', 'dirt', 'dead shrub']
),
[]
);
```
`["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]` should return an empty array.
```js
assert(
diffArray(
['andesite', 'grass', 'dirt', 'dead shrub'],
['andesite', 'grass', 'dirt', 'dead shrub']
).length === 0
);
```
`[1, 2, 3, 5], [1, 2, 3, 4, 5]` should return `[4]`.
```js
assert.sameMembers(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]), [4]);
```
`[1, 2, 3, 5], [1, 2, 3, 4, 5]` should return an array with one item.
```js
assert(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]).length === 1);
```
`[1, "calf", 3, "piglet"], [1, "calf", 3, 4]` should return `["piglet", 4]`.
```js
assert.sameMembers(diffArray([1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]), [
'piglet',
4
]);
```
`[1, "calf", 3, "piglet"], [1, "calf", 3, 4]` should return an array with two items.
```js
assert(diffArray([1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]).length === 2);
```
`[], ["snuffleupagus", "cookie monster", "elmo"]` should return `["snuffleupagus", "cookie monster", "elmo"]`.
```js
assert.sameMembers(diffArray([], ['snuffleupagus', 'cookie monster', 'elmo']), [
'snuffleupagus',
'cookie monster',
'elmo'
]);
```
`[], ["snuffleupagus", "cookie monster", "elmo"]` should return an array with three items.
```js
assert(diffArray([], ['snuffleupagus', 'cookie monster', 'elmo']).length === 3);
```
`[1, "calf", 3, "piglet"], [7, "filly"]` should return `[1, "calf", 3, "piglet", 7, "filly"]`.
```js
assert.sameMembers(diffArray([1, 'calf', 3, 'piglet'], [7, 'filly']), [
1,
'calf',
3,
'piglet',
7,
'filly'
]);
```
`[1, "calf", 3, "piglet"], [7, "filly"]` should return an array with six items.
```js
assert(diffArray([1, 'calf', 3, 'piglet'], [7, 'filly']).length === 6);
```
# --seed--
## --seed-contents--
```js
function diffArray(arr1, arr2) {
var newArr = [];
return newArr;
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
```
# --solutions--
```js
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;
}
```