Intermediate Algorithm Scripting - Sorted Union

Tell us what’s happening:
Describe your issue in detail here.
I wanted to solve this challenge by combining all the arguments into an array and then removing the duplicate elements of the array . I know my code is not right but this is what I typed as per my knowledge. Can anyone guide me ?

Your code so far

function uniteUnique(arr) {
  let newArr = [arguments];
  for (let i = 0; i < newArr.length; i++){
    for (let j = 0; j < newArr.length; j++){
      if(newArr[i] === newArr[j]){
        delete newArr[j];
      }
}
  }
  return newArr;
}

console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]));
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Sorted Union

Link to the challenge:

let newArr = [arguments];

Line above a bit confusing, ensure console.log it to see what is your newArr.
Anyway, i believe this is what you should start with :smile:

let newArr = [...arguments].flat()
1 Like

thanks a lot for this tip … now how can I delete the duplicates ? I guess Another way could be pushing the does not include elements in another array but I want to delete the duplicates from the newArr.

function uniteUnique(arr) {
  let newArr = [...arguments].flat();
  console.log(newArr);
  for (let i = 0; i < newArr.length; i++){
    for (let j = 0; j < newArr[i].length; j++){
      if(newArr[i] === newArr[i][j]){
        delete newArr[j];
      }
}
  }
  return newArr;
}

console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]));

delete newArr[j];
// avoid this, this would leave empty hole in array

Why don’t you try with array.splice(). You can specify the location and number of item to remove without leaving hole in array.

About splice you can read more here JavaScript Array splice() Method

2 Likes

Note - changing an array as you loop over it is a recepie for bugs

thank You Wong and Jeremy for all your help I completed the challenge . You both have been a great help for me since quite a time. See You at next Doubt :sweat_smile:.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.