Tell us what’s happening:
Hi, so I was able to write the code so far but I don’t understand why the code is splicing out the 4 when it’s not a duplicate. Maybe I’m not looking at it correctly, but any help would be greatly appreciated, thank you for your time.
Your code so far
function uniteUnique(arr) {
var args = Array.prototype.slice.call(arguments, 0)
let args1 = [].concat.apply([], args)
let i=1;
for (let j=0; j<args1.length; j++){
if (args1[i] === args1[j]) {
args1.splice(i, args1.length);
}
if (args1[i] !== args1[j]) {
i++
}
}
return args1
}
console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36.
function uniteUnique(arr) {
var args = Array.prototype.slice.call(arguments, 0)
console.log(args)
//here i i'm combing all of the parameters into a mutlidimensional array
let args1 = [].concat.apply([], args)
//and here i'm joining all of the individual arrays into one array
let i=args1.length-1;
//i will be my right pointer starting at the end of the array
for (let j=0; j<args1.length; j++){
//j is my left pointer starting at the beginning of the array. The loop runs for the entirety of the args1.length
let duplicate = args1[i] === args1[j];
// this is a variable assigned to see if the number at the end of args1 and the beginning of args1 are the same
if (duplicate) {
args1.splice(i, args1.length);
//if duplicate exists then remove that number at variable i, and the nnumber of removals depends on the length of args1
}
if (args1[i] !== args1[j]) {
i--;
//this says if the two numbers don't equal each other decrement i and j will increment because of the loop
}
}
return args1;
}
console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]));