2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
title: Diff Two Arrays
|
|
|
|
---
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
# Diff Two Arrays
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Problem Explanation
|
2018-10-12 15:37:13 -04:00
|
|
|
|
|
|
|
Check two arrays and return a new array that contains only the items that are not in either of the original arrays.
|
|
|
|
|
|
|
|
#### Relevant Links
|
|
|
|
|
|
|
|
* <a href='https://devdocs.io/javascript/statements/for' target='_blank' rel='nofollow'>for Loop (Devdocs)</a>
|
|
|
|
* <a href='https://devdocs.io/javascript/global_objects/array/includes' target='_blank' rel='nofollow'>Array.prototype.includes (Devdocs)</a>
|
|
|
|
* <a href='https://devdocs.io/javascript/global_objects/array/filter' target='_blank' rel='nofollow'>Array.prototype.filter (Devdocs)</a>
|
|
|
|
* <a href='https://devdocs.io/javascript/global_objects/array/concat' target='_blank' rel='nofollow'>Array.prototype.concat (Devdocs)</a>
|
|
|
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Hints
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
### Hint 1
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
Merge the list to make it easy to compare functions.
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
### Hint 2
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
Use filter to get the new array, you will need to create a callback function.
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
### Hint 3
|
2018-10-12 15:37:13 -04:00
|
|
|
|
|
|
|
The best way to go about the callback function is to check if the number from the new merged array is not in **both** original arrays and return it.
|
|
|
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Solutions
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
(Imperative Solution)
|
2018-10-12 15:37:13 -04:00
|
|
|
```javascript
|
2019-07-24 00:59:27 -07:00
|
|
|
function diffArray(arr1, arr2) {
|
|
|
|
var newArr = [];
|
|
|
|
|
|
|
|
function onlyInFirst(first, second) {
|
|
|
|
// Looping through an array to find elements that don't exist in another array
|
|
|
|
for (var i = 0; i < first.length; i++) {
|
|
|
|
if (second.indexOf(first[i]) === -1) {
|
|
|
|
// Pushing the elements unique to first to newArr
|
|
|
|
newArr.push(first[i]);
|
2018-10-12 15:37:13 -04:00
|
|
|
}
|
2019-07-24 00:59:27 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
onlyInFirst(arr1, arr2);
|
|
|
|
onlyInFirst(arr2, arr1);
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
return newArr;
|
|
|
|
}
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
|
2018-10-12 15:37:13 -04:00
|
|
|
```
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
#### Code Explanation
|
2018-10-12 15:37:13 -04:00
|
|
|
|
|
|
|
Read the comments in the code.
|
|
|
|
|
|
|
|
#### Relevant Links
|
|
|
|
|
|
|
|
* <a href='https://devdocs.io/javascript/statements/for' target='_blank' rel='nofollow'>for Loop (Devdocs)</a>
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
</details>
|
|
|
|
|
|
|
|
<details><summary>Solution 2 (Click to Show/Hide)</summary>
|
|
|
|
|
|
|
|
|
|
|
|
(Declarative Solution):
|
2018-10-12 15:37:13 -04:00
|
|
|
```javascript
|
2019-07-24 00:59:27 -07:00
|
|
|
function diffArray(arr1, arr2) {
|
|
|
|
return arr1
|
|
|
|
.concat(arr2)
|
|
|
|
.filter(item => !arr1.includes(item) || !arr2.includes(item));
|
|
|
|
}
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
|
2018-10-12 15:37:13 -04:00
|
|
|
```
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
#### Code Explanation
|
2018-10-12 15:37:13 -04:00
|
|
|
|
|
|
|
Explain solution here and add any relevant links
|
|
|
|
|
|
|
|
#### Relevant Links
|
|
|
|
|
|
|
|
* <a href='https://devdocs.io/javascript/global_objects/array/concat' target='_blank' rel='nofollow'>Array.prototype.concat (Devdocs)</a>
|
|
|
|
* <a href='https://devdocs.io/javascript/global_objects/array/filter' target='_blank' rel='nofollow'>Array.prototype.filter (Devdocs)</a>
|
|
|
|
* <a href='https://devdocs.io/javascript/global_objects/array/includes' target='_blank' rel='nofollow'>Array.prototype.includes (Devdocs)</a>
|
2019-07-24 00:59:27 -07:00
|
|
|
</details>
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
<details><summary>Solution 3 (Click to Show/Hide)</summary>
|
|
|
|
|
|
|
|
(Declarative Solution)
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
function diffArray(arr1, arr2) {
|
|
|
|
return [...diff(arr1, arr2), ...diff(arr2, arr1)];
|
|
|
|
|
|
|
|
function diff(a, b) {
|
|
|
|
return a.filter(item => b.indexOf(item) === -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2018-10-12 15:37:13 -04:00
|
|
|
|
|
|
|
#### Relevant Links
|
|
|
|
|
|
|
|
* <a href='https://devdocs.io/javascript/global_objects/array/includes' target='_blank' rel='nofollow'>Array.prototype.includes (Devdocs)</a>
|
|
|
|
* <a href='https://devdocs.io/javascript/global_objects/array/filter' target='_blank' rel='nofollow'>Array.prototype.filter (Devdocs)</a>
|
|
|
|
* <a href='https://devdocs.io/javascript/global_objects/array/concat' target='_blank' rel='nofollow'>Array.prototype.concat (Devdocs)</a>
|
2019-07-24 00:59:27 -07:00
|
|
|
</details>
|
2018-10-12 15:37:13 -04:00
|
|
|
|