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 ?

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

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

``````let newArr = [arguments];
``````

Line above a bit confusing, ensure console.log it to see what is your newArr.

``````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.