Hi all,
Quick question. I tried solving the problem in 2 different ways. One of them was with a for loop. I tried using concat inside my for loop (trying to add numbers to an arr), but it wouldn’t work. I was trying to understand the difference between using push() and concat () for this particular case.
My second option was with filter, and concat did work.
Any guidance is greatly appreciated.
Using For Loop
function uniteUnique(arr) {
let allArgs = [...arguments];
let finalArr = allArgs.shift();//keeps fist arr
for(let i =0; i<allArgs.length; i++){
for (let j = 0; j < allArgs[i].length; j++){
if(!arr.includes(allArgs[i][j])){
finalArr.push(allArgs[i][j]);
}
}
}
return finalArr;
}
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
Using Filter()
function uniteUnique(arr) {
let allArgs = [...arguments];
let finalArr = allArgs.shift();
let joinRemainingArrs = Array.prototype.concat(...allArgs);
//console.log(joinRemainingArrs);
let noDuplicates = joinRemainingArrs.filter(x=> !arr.includes(x)? x: null);
return finalArr.concat(noDuplicates);
}
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);