Sorted Union, how?

So i’m doing the Sorted Union challenge, in which this is the starting code.

challenge → https://www.freecodecamp.org/challenges/sorted-union

function uniteUnique(arr) {
  return arr;
}

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

The instructions read:

Write a function that takes two or more arrays and returns a
new array of unique values in the order of the original provided arrays.

In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.

The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.


So what the instructions mean is that i’m suppose to take all 3 arrays and get unique values and return them in order in their original array order.

My question is, how am i suppose to get all 3 arrays if i have one parameter? Its not a 2D array and i wont know how many arrays the input is, so i cant just put 3 parameters. When i run the code listed above i get:

[1, 3, 2] (1/3 arrays)


Any ideas on how i get all 3 arrays with 1 parameter. I know you can change teh starting code, but the requirements dont use 2D arrays for there input…

What i mean by 2D arrays is all the input arrays in 1 big array so i can access all 3 at once. Am i missing something with this?

There are some links on the page under “Helpful Links”. One of the helpful links is particularly helpful: arguments object.

1 Like

Thanks, i saw the article, but didn’t quite understand what it was talking about.

Basically, when you run a function, an arguments object is created with all of the objects that were passed to the function.

If you have a function

function foo(bar) {
   return baz
}

Bar, in this case, is defined, but if you pass more than one argument into the function javascript will store that extra argument (as well as any other arguments) in what is known as an argument object.

Most people convert this argument object into an array using these lines of code (these are three different ways of converting, only one line is necessary):

var args = Array.prototype.slice.call(arguments);
var args = [].slice.call(arguments);

// ES2015

const args = Array.from(arguments)

From here you can access any arguments that were passed to the function even if you only specified one parameter.

Hope that helps @John-freeCodeCamp

1 Like

Yeah i was able to get the arrays. The reason i didn’t get it when i started the challange is because the article made no sense.

Oh okay, I didn’t read the article at all I was just responding because from your reaction to the article it didn’t seem like your problem was solved glad you figured it out though!

1 Like

I have a question. For the requirements, is [5] treated as a different value from 5 or are they the same?

I’m not really sure what you mean; however, in javascript [5] is an array with a value of 5 at index 0, 5 is a number so they are very different. If you’re ever wondering if something is the same console.log the two values with a strict equal ‘===’ in between them and if they are the same they will log true, false otherwise.

If i were to sort this, would i include it with the 5's?

So if you sort this you need one of every number that you encounter in the order that you encountered it in. That’s what I’m gathering from the instructions.

So would i separate [5] from 5?

If you want to reference something inside of an array, you would have to do so with a variable so:

var array = [5];
console.log(array[0]);

This would log the number five.

I know that, but with [1, 3, 2], [5, 2, [3], 4], [3, 1], would i put 3 in my final array since 3 is present through the whole thing or not 3 but just [3] because one is a array and one isnt?

I don’t think the returned array is supposed to be two dimensional. As far as I’m reading it seems like you should return an array of only numbers, not an array that has another array inside of it. So if you already have 3 in your returned array not only do you not need to add an array [3] but you shouldn’t add another 3 value either.

uniteUnique([1, 3, 2], [1, [5]], [2, [4]]) should return [1, 3, 2, [5], [4]].

Plus why does it say uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]) should return [1, 2, 3, 5, 4, 6, 7, 8].

if 1 is only present in 75% of the arrays?

Okay, I went and looked at the challenge and yes your array can be two dimensional. As far as your last question it looks like they just need to be unique values, not values that appear in every array.

But wouldnt a unique value only appear in one of all of the arrays?

Or am i just returning one of each number used?

Um, technically yes; however, you’re thinking about the wrong array. The array you return needs to have unique values. The challenge is to find values within each array and put it in another array. If the array you’re storing everything in already has the value you’re trying to store you want to skip that value.

You’ve got an array of arrays containing arbitrary values. You need to merge them into a single array and remove any duplicates. The values can be numbers or arrays. [5] is not the same as 5.