.filter(el => !arr2.includes(el))
what does ! before arr mean?
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,
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]);
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]);
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?
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]);