* fix: restructure certifications guide articles * fix: added 3 dashes line before prob expl * fix: added 3 dashes line before hints * fix: added 3 dashes line before solutions
3.4 KiB
3.4 KiB
title
title |
---|
Diff Two Arrays |
Diff Two Arrays
Problem Explanation
Check two arrays and return a new array that contains only the items that are not in either of the original arrays.
Relevant Links
- for Loop (Devdocs)
- Array.prototype.includes (Devdocs)
- Array.prototype.filter (Devdocs)
- Array.prototype.concat (Devdocs)
Hints
Hint 1
Merge the list to make it easy to compare functions.
Hint 2
Use filter to get the new array, you will need to create a callback function.
Hint 3
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
Solution 1 (Click to Show/Hide)
(Imperative Solution)
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]);
}
}
}
onlyInFirst(arr1, arr2);
onlyInFirst(arr2, arr1);
return newArr;
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
Code Explanation
Read the comments in the code.
Relevant Links
Solution 2 (Click to Show/Hide)
(Declarative Solution):
function diffArray(arr1, arr2) {
return arr1
.concat(arr2)
.filter(item => !arr1.includes(item) || !arr2.includes(item));
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
Code Explanation
Explain solution here and add any relevant links
Relevant Links
Solution 3 (Click to Show/Hide)
(Declarative Solution)
function diffArray(arr1, arr2) {
return [...diff(arr1, arr2), ...diff(arr2, arr1)];
function diff(a, b) {
return a.filter(item => b.indexOf(item) === -1);
}
}