Files

122 lines
3.4 KiB
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Diff Two Arrays
---
# Diff Two Arrays
2018-10-12 15:37:13 -04: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>
---
## Hints
2018-10-12 15:37:13 -04:00
### Hint 1
2018-10-12 15:37:13 -04:00
Merge the list to make it easy to compare functions.
2018-10-12 15:37:13 -04:00
### Hint 2
2018-10-12 15:37:13 -04:00
Use filter to get the new array, you will need to create a callback function.
2018-10-12 15:37:13 -04: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.
---
## Solutions
2018-10-12 15:37:13 -04:00
<details><summary>Solution 1 (Click to Show/Hide)</summary>
2018-10-12 15:37:13 -04:00
(Imperative Solution)
2018-10-12 15:37:13 -04:00
```javascript
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
}
}
}
2018-10-12 15:37:13 -04:00
onlyInFirst(arr1, arr2);
onlyInFirst(arr2, arr1);
2018-10-12 15:37:13 -04:00
return newArr;
}
2018-10-12 15:37:13 -04:00
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
2018-10-12 15:37:13 -04: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>
</details>
<details><summary>Solution 2 (Click to Show/Hide)</summary>
(Declarative Solution):
2018-10-12 15:37:13 -04:00
```javascript
function diffArray(arr1, arr2) {
return arr1
.concat(arr2)
.filter(item => !arr1.includes(item) || !arr2.includes(item));
}
2018-10-12 15:37:13 -04:00
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
2018-10-12 15:37:13 -04: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>
</details>
2018-10-12 15:37:13 -04: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>
</details>
2018-10-12 15:37:13 -04:00