Am I over-simplifying Sorted Union?

Tell us what’s happening:

I just wanted to lay out my approach in the beginning. It seems like a pretty straight-forward challenge, but…I don’t know.

Your code so far

function uniteUnique(arr) {
  //function taking 2 or more arrays returns one array of unique values in order of provided arrays
  //1.Use 2-dimensional loop
  //2.Return statement compares different iterations to determine if element has already appeared.  
  //3.If it has not, then it is pushed into new array.

  return arr;

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/71.0.3578.98 Safari/537.36.

Link to the challenge:

Your pseudo-code looks like it will work in terms of looping through every element of the array input and keeping the original order of the unique elements in a new array.

I personally don’t like the approach. It would require you to do multiple searches.

If you had a billion elements… you would be required to search a billion elements for each new element.

If you’re familiar with key / value of an object,. you’d know that you can only have one unique key for each value.

Meaning if i had this:

let previous = {};
previous[4] = true;

then did,…

previous[7] = true;
previous[9] = true;
previous[4] = true;

Notice none of these inserts require searching the previous object. You just overwrite the key.

The final product being one of each.

//Comments and concerns below the code.
function uniteUnique(arr) {
 //Create empty container
 var newArr = [];
 //Arrange all possible values into single array
 var args = [...arguments];
 var len = args.length
 //Use filter method to sift out unique elements
   for(var i = 0; i < len; i++){
//This is what I'm using to determine unique elements 
if(val[i] != val[i]){
//This shows nothing on my console
 //I don't pass anything
  return newArr;

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

/*On a superficial level, it seems to me it should work. I'll state why I believe this below (1). Then, below that some doubts I have about this naivete(2).  I would like holes blown in my logic wherever necessary...

    What this challenge asks for is a search for non-duplicates.  Anything that doesn't appear more than once is accepted and there for funneled into the empty vessel.  I thought filter method would be perfect for this as it would return push those values that only appeared once into the empty container.

   a. I had doubts that filter() would work as filter() is primarily a boolean method, correct?  It only determines if something is true or false.  I did try map() as well but nothing happened there is well. 
   b. I think that my method for determining on-duplicates is fraught with error.  val[i] !== val[i] seems to repeat itself as val is, in fact, i, correct?
   c. I am concerned that using [...arguments] is interfering with the process.  I'm not sure why I feel this way, but perhaps adjusting the character of the different subarrays affects how the console is receiving my answer.  (In terms of, this console is built toward receiving an answer completed in a certain way meant to teach some aspect of Javascript?)

Just a question… do you want to have a future career in programming?

This is absolutely not what you are doing. You are not using filter. It is just there as decoration.

Refer to this please… (if you don’t want to return Anything inside the method, use forEach, each method has its applications, if you’re unsure, the documentation is there for you)

This is false, something is not different from itself. Obviously nothing is pushed.

Well, console.log the hell out of everything else… each variable for example, after its declaration, after it is changed in some way. Before an if statement console.log() the comparison you put inside the if statement

We can point out if you use the wrong method and such, but be a master of console.log() otherwise you will never know if your code does what you think it should do

1 Like

So that was an incredibly flawed approach.
I seem to constantly misunderstand filter() so I’m REALLY going to have to sit down with this method again and again.

Anyway, my idea, given your comments, is that the best approach would be to use a basic for loop and push the first instance of every element into an empty container. Then, to return said container.
That seems pretty solid to me. I’m working on the code right now but the basic idea…that’s good, right?

That’s good
Please practice console.log() with every variable you use so that you can be sure of what your code is doing

dont use arguments

Why not? That allows me to access all elements that otherwise console.log(arr) does not register.

Almost there, just use an other way to add the elements to newArray because concat() returns a new array, doesn’t change the array on which is applied

function uniteUnique(...arr) {
     return arr.reduce((a, b) => a.concat(b), [])
               .filter((element, index, array) => array.indexOf(element) === index);

First, combine all the elements into a single array with reduce.
Second, filter out duplicates using filter.

It is awesome if you want to help, but the original poster wanted to arrive there on their own.

It is better if you give hints instead of the solution

And if you give the solution please hide it inside the [spoiler][/spoiler] tags


The thread is a week old. Relax.

Also, the OP states nowhere he/she wanted to arrive there on his/her own. You’re assuming that.

There is a guide with the solutions, if someone want the solution they can copy and paste from there

True., yet people still come to forums looking for solutions.

Could you please wrap it in the spoiler tags, so if someone want the solution they can read it, if someone is just searching for hints they will not have the solution jump them on surprise?

What you’re asking for is reasonable. So I’ll do that for now on.

1 Like

Good. Just to reiterate what Leah’s saying is consistent with the intent of the forum. But you seem to get that now, so cool. I’ve added spoiler tags to your answer.

Thank you all commenters. I was able to solve it.