# freeCodeCamp Challenge Guide: Diff Two Arrays

freeCodeCamp Challenge Guide: Diff Two Arrays
0

Hi @sgenio

This is my simple solution

function diffArray(a, b){
c = a.concat(b)
d = [];
var diffarr = c.filter(function(c1){
if (a.indexOf(c1) === -1 || b.indexOf(c1) === -1){
d.push(c1);
}
});
return d;
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

Hi Friends

This is my solution

function diffArray(a, b){
c = a.concat(b)
d = [];
var diffarr = c.filter(function(c1){
if (a.indexOf(c1) === -1 || b.indexOf(c1) === -1){
d.push(c1)
}
})
return d
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

## .filter(el => !arr2.includes(el))

what does ! before arr mean?

Here’s My solution using indexOf() . Seems simple to understand. Well, hint helped me:sweat_smile:[spoiler]This text will be blurred

function diffArray(arr1, arr2) {
var newArr = [];
// Same, same; but different.

var x = arr1.concat(arr2);

for(var i = 0 ; i<x.length; i++){
if(arr1.indexOf(x[i]) === -1 || arr2.indexOf(x[i]) === -1 )
newArr.push(x[i]);
}

return newArr;
}

diffArray([1, "calf", 3, "piglet"], [1, "calf", 3, 4]);

[/spoiler]

Here’s My solution using indexOf() . Seems simple to understand. Well, hint helped me [spoiler]

function diffArray(arr1, arr2) {
var newArr = [];
// Same, same; but different.

var x = arr1.concat(arr2);

for(var i = 0 ; i<x.length; i++){
if(arr1.indexOf(x[i]) === -1 || arr2.indexOf(x[i]) === -1 )
newArr.push(x[i]);
}

return newArr;
}

diffArray([1, "calf", 3, "piglet"], [1, "calf", 3, 4]);

[/spoiler]

function diffArray(arr1, arr2) {
return [...arr1, ...arr2].filter(item => !arr1.includes(item) || !arr2.includes(item));
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

Had to read the hints to figure this out. Solution ended up being more or less Intermediate solution but not as optimized

function diffArray(arr1, arr2) {
var arr = [];
var newArr=[];
// Same, same; but different.
arr = arr1.concat(arr2);

newArr = arr.filter(function(x){
if((arr1.indexOf(x)===-1) || (arr2.indexOf(x)===-1)){
return x;
}
});
return newArr;
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

I was originally writing something basically like this,

1. merge and sort it out
2. Delete duplicates
3. Push to new array if conditions are met

but I couldn’t get the IF statement line to work quite right, I had most of it down ended up scraping it once i read the hints

e.g.

[1,1,2,2,3,3,4,5,5]

If the value [4] has no adjacent [4] next to it it must be unique

function diffArray(arr1, arr2) {

var newArr = [];

function resultDiffArray(a1,a2){
for(var i=0;i<a1.length;i++){
if(a2.indexOf(a1[i])==-1){
newArr.push(a1[i]);
}
}
}

resultDiffArray(arr1,arr2);
resultDiffArray(arr2,arr1);

return newArr;
}

So I’m really new to this, and to be honest I wasn’t expecting this code to work but it did…I’m not really sure if I fully understand why it worked.

function diffArray(arr1, arr2) {
var newArr = [];
var otherArr= [];
newArr = arr1.concat(arr2);
for (var i = 0; i < newArr.length; i++) {
otherArr = newArr.filter(function(i){
return arr1.indexOf(i) < 0 || arr2.indexOf(i) < 0;
});
}
return otherArr;
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

Simplified, with help from rmdawson71. This works!

function diffArray(arr1, arr2) {
return arr1.concat(arr2).filter(item=>!arr1.includes(item) || !arr2.includes(item)
);
}

my code

function diffArray(arr1, arr2) {
var newArr,temp,temp1;
// Same, same; but different.

temp=arr1.filter(function(el)
{
return arr2.indexOf(el) == -1;

});

temp1=arr2.filter(function(el)
{
return arr1.indexOf(el) == -1;

});

newArr=temp.concat(temp1);

return newArr;

}

diffArray([1, 2, 12, 3, 5], [1, 2, 3, 4, 5]);

Great works my friend!

1 Like

Since my understanding of filter function is really poor I came up with this:

function diffArray(arr1, arr2) {
var newArr = [];
var result = [];
// Same, same; but different.

newArr = arr1.concat(arr2);

for (var i=0; i<newArr.length; i++) {

if (arr1.includes(newArr[i]) && !arr2.includes(newArr[i]) || !arr1.includes(newArr[i]) && arr2.includes(newArr[i])) {

result.push(newArr[i]);
console.log(result);

}

}

return result;
}

diffArray(['a', 'b', 'c', 'e'], ['a', 'b', 'c', 'd', 'e']);

my solution is a bit different I guess, using concat and splice

function diffArray(arr1, arr2) {
var newArr = [];
newArr = arr1.concat(arr2).sort(); //combine the two arrays in one and sort them
for(i=0;i<newArr.length;i++){ //loop through the full new array
if(newArr[i] == newArr[i+1]){ //if two consecutive elements are similar, delete them both
newArr.splice(i,2);
i = i-2;//after deleting for example element 0 and 1 while the loop is at count 0, next when it goes to element 1 it will be the old element 3 because 2 elements are gone, so if we remove two we count two steps back
}
}
return newArr;
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
1 Like
function diffArray(arr1, arr2) {
// Same, same; but different.

// Setup temp array to hold arr1 since filter() will change contents of arr1
var tempArr1 = arr1;
return arr1.filter(item => arr2.indexOf(item) === -1).concat(arr2.filter(item => tempArr1.indexOf(item) === -1));
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

This was my solution. Thoughts?

1 Like

Hello,

Could anyone explain me the solution below? I am not sure if I get what exactly those 3 dots do. I think they are “spread syntax” but still don’t understand how it works.

function diffArray(arr1, arr2) {
return [
...diff(arr1, arr2),
...diff(arr2, arr1)
]

function diff(a, b) {
return a.filter(item => b.indexOf(item) === -1);
}
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

If you still have questions, create a new topic with your specific questions related to what you do not understand about using the spread syntax.

A post was split to a new topic: I have a problem with my code for Diff Two Arrays