2018-10-04 14:37:37 +01:00
---
id: a5de63ebea8dbee56860f4f2
title: Diff Two Arrays
isRequired: true
challengeType: 5
2020-05-21 17:31:25 +02:00
isHidden: false
2019-07-31 11:32:23 -07:00
forumTopicId: 16008
2018-10-04 14:37:37 +01:00
---
## Description
< section id = '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.
< strong > Note< / strong > < br > You can return the array with its elements in any order.
< / section >
## Instructions
< section id = 'instructions' >
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
2018-10-20 21:02:47 +03:00
- text: < code > diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])</ code > should return an array.
2019-07-24 01:56:38 -07:00
testString: assert(typeof diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]) === "object");
2018-10-20 21:02:47 +03:00
- text: < code > ["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]</ code > should return < code > ["pink wool"]</ code > .
2019-07-24 01:56:38 -07:00
testString: assert.sameMembers(diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]), ["pink wool"]);
2018-10-20 21:02:47 +03:00
- text: < code > ["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]</ code > should return an array with one item.
2019-07-24 01:56:38 -07:00
testString: assert(diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]).length === 1);
2018-10-20 21:02:47 +03:00
- text: < code > ["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]</ code > should return < code > ["diorite", "pink wool"]</ code > .
2019-07-24 01:56:38 -07:00
testString: assert.sameMembers(diffArray(["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]), ["diorite", "pink wool"]);
2018-10-20 21:02:47 +03:00
- text: < code > ["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]</ code > should return an array with two items.
2019-07-24 01:56:38 -07:00
testString: assert(diffArray(["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]).length === 2);
2018-10-20 21:02:47 +03:00
- text: < code > ["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]</ code > should return < code > []</ code > .
2019-07-24 01:56:38 -07:00
testString: assert.sameMembers(diffArray(["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]), []);
2018-10-20 21:02:47 +03:00
- text: < code > ["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]</ code > should return an empty array.
2019-07-24 01:56:38 -07:00
testString: assert(diffArray(["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]).length === 0);
2018-10-20 21:02:47 +03:00
- text: < code > [1, 2, 3, 5], [1, 2, 3, 4, 5]</ code > should return < code > [4]</ code > .
2019-07-24 01:56:38 -07:00
testString: assert.sameMembers(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]), [4]);
2018-10-20 21:02:47 +03:00
- text: < code > [1, 2, 3, 5], [1, 2, 3, 4, 5]</ code > should return an array with one item.
2019-07-24 01:56:38 -07:00
testString: assert(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]).length === 1);
2018-10-20 21:02:47 +03:00
- text: < code > [1, "calf", 3, "piglet"], [1, "calf", 3, 4]</ code > should return < code > ["piglet", 4]</ code > .
2019-07-24 01:56:38 -07:00
testString: assert.sameMembers(diffArray([1, "calf", 3, "piglet"], [1, "calf", 3, 4]), ["piglet", 4]);
2018-10-20 21:02:47 +03:00
- text: < code > [1, "calf", 3, "piglet"], [1, "calf", 3, 4]</ code > should return an array with two items.
2019-07-24 01:56:38 -07:00
testString: assert(diffArray([1, "calf", 3, "piglet"], [1, "calf", 3, 4]).length === 2);
2018-10-20 21:02:47 +03:00
- text: < code > [], ["snuffleupagus", "cookie monster", "elmo"]</ code > should return < code > ["snuffleupagus", "cookie monster", "elmo"]</ code > .
2019-07-24 01:56:38 -07:00
testString: assert.sameMembers(diffArray([], ["snuffleupagus", "cookie monster", "elmo"]), ["snuffleupagus", "cookie monster", "elmo"]);
2018-10-20 21:02:47 +03:00
- text: < code > [], ["snuffleupagus", "cookie monster", "elmo"]</ code > should return an array with three items.
2019-07-24 01:56:38 -07:00
testString: assert(diffArray([], ["snuffleupagus", "cookie monster", "elmo"]).length === 3);
2018-10-20 21:02:47 +03:00
- text: < code > [1, "calf", 3, "piglet"], [7, "filly"]</ code > should return < code > [1, "calf", 3, "piglet", 7, "filly"]</ code > .
2019-07-24 01:56:38 -07:00
testString: assert.sameMembers(diffArray([1, "calf", 3, "piglet"], [7, "filly"]), [1, "calf", 3, "piglet", 7, "filly"]);
2018-10-20 21:02:47 +03:00
- text: < code > [1, "calf", 3, "piglet"], [7, "filly"]</ code > should return an array with six items.
2019-07-24 01:56:38 -07:00
testString: assert(diffArray([1, "calf", 3, "piglet"], [7, "filly"]).length === 6);
2018-10-04 14:37:37 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function diffArray(arr1, arr2) {
var newArr = [];
return newArr;
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function diffArray(arr1, arr2) {
var newArr = [];
var h1 = Object.create(null);
arr1.forEach(function(e) {
h1[e] = e;
});
2018-10-08 01:01:53 +01:00
2018-10-04 14:37:37 +01:00
var h2 = Object.create(null);
arr2.forEach(function(e) {
h2[e] = e;
});
2018-10-08 01:01:53 +01:00
2018-10-04 14:37:37 +01:00
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;
}
```
< / section >