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) {
const newArr = [];
function onlyInFirst(first, second) {
// Looping through an array to find elements that don't exist in another array
for (let 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]);
I don’t know if this is a suitable solution since I’m still a newbie, but it works, without using indexOf and filter methods. I do not know if it is a basic, intermediate or advanced solution.
function diffArray(arr1, arr2) {
var newArr = [];
//concatenate the two arrays and sort the items in growing order
var ordArr = arr1.concat(arr2).sort();
//loop through the ordered array and push to the empty newArr only the items that have not a twin in the ordered arr (only items that are not equal to the previous nor to the next item)
for(var i=0; i<ordArr.length; i++) {
if(ordArr[i] !== ordArr[i+1] && ordArr[i] !== ordArr[i-1]){
newArr.push(ordArr[i]);
}
}
return newArr;
}
for(var j = 0; j<arr2.length; j++){
if(arr1[i] === arr2[j]){
delete arr1[i]; // remove common elements from arr1 and arr2
delete arr2[j];
}
}
}
newArr = (arr1.filter(Boolean)).concat(arr2.filter(Boolean)); // concat the two arrays after removing the Boolean values like null, undefined which may have been there after deleting the array elements
return newArr;
}
function diffArray(arr1, arr2) {
// filter each array for the absent members
var filteredArr1 = arr1.filter((el) => arr2.indexOf(el) === -1),
filteredArr2 = arr2.filter((el) => arr1.indexOf(el) === -1);
// merge both sets of absent members
return filteredArr1.concat(filteredArr2);
}
Hi I really like your solution, especially that it is much shorter than mine. But I see syntax like .filter((el) => arr…)
And I dont mean filter method, but => statement. What is it? How to call it and where can I find any tutorial about it?
var newArr = arr1.concat(arr2); //concat both arrays sorting
var singledout=[];
for (i=0;i<newArr.length;i++)
{
if (newArr.indexOf(newArr[i])==newArr.lastIndexOf(newArr[i]))
{
singledout.push(newArr[i]); //add item only if its once in the array
}
}
function diffArray(arr1, arr2) {
return arr1 //returns the final iteration of the array
.concat(arr2) //combines arr1 and arr2 into a new array
.filter( //creates a new array with elements that pass a test
item => //this is a shorthand for a function expression
!arr1.includes(item) //this is a test that only passes elements that are NOT included in arr1
|| !arr2.includes(item) //this is a test that only passes elements that are NOT included in arr2
)
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
I found it tricky to understand how the intermediate solution actually worked and the elements involved in it. Therefore I broke down each step and tried to explain it in comments. This has helped me to understand the more complex code going on here. Could someone please let me know if any of my explanations are incorrect and if this is the right way to contribute.
I have also gone through the advanced code solution, adding comments. It seems that the only differences between the intermediate and advanced solutions are:
that “item” has been replaced with “el” which suggests that these are labels that can be almost anything you like.
The filtering of arr1 has happened before the concat element which means that filter has to be called twice and there is no need for an “or ||” call.
It would appear that the advanced solution is a little less intuitive and demonstrates that ordering is not as fixed as it seems.
function diffArray(arr1, arr2) { return arr1 //returns the final iteration of the array .filter(el => !arr2.includes(el)) //filters out all elements from arr1 except those that are not in arr2 .concat( //combines filtered arr1 with the soon-to-be filtered arr2 arr2.filter(el => !arr1.includes(el)) //filters out all elements from arr2 except those that are not in arr1 ) } diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
Try looking to functional programming and higher order function. You will be amazed that how easier your life be with functional programming and loops will be in lower priority.
I’d like to see something besides the Basic Code Solution that doesn’t include whatever => is, since freeCodeCamp hasn’t taught => at this point. I’m trying to understand just how .filter works, so a solution that included something like that, but didn’t include things that freeCodeCamp hasn’t covered at all yet so that it would be possible to do it without going outside the bounds of what an actual beginner learning from freeCodeCamp could do, would be helpful.