Sorted Union: Intermediate Algorithms help

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.

Challenge: Sorted Union

Link to the challenge:

Hey there,

nice to meet you,
good work so far!

So can you write a comment above each line of code and describe what this line should do?

working with code you posted …

function uniteUnique(arr) {
arr = Array.prototype.slice.call(arguments, 0);
console.log(arr)//(3) [Array(3), Array(4), Array(2)]
arr = [].concat.apply([],arr)
console.log(arr)//[1, 3, 2, 5, 2, 1, 4, 2, 1]
 //now just return a new Set 
}
console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]));

Hi,
Nice to meet you too!

Here is the code with comments,

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]));

Hey @Da_vey and @Edocsil,

I’ve edited your posts for readability.
Please use a code block when you want to publish code.

Precede your code with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor ( </> ) to add backticks around text.

See this post to find the backtick on your keyboard. Backticks (`) are not single quotes (’).

Thank You!, I often require supervision.

:grin:

1 Like