** Explanation Needed ** Sorted Union

Link to challenge:

Solution:

function uniteUnique(arr) {
  var args = [...arguments];
  var result = [];

  for (let i = 0; i < args.length; i++) {
    for (let j = 0; j < args[i].length; j++) {
      if (!result.includes(args[i][j])) {
        result.push(args[i][j]);
      }
    }
  }
  return result;
}

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

Could someone please explain to me what the following code means? I found this solution from the challenge Guide but it doesn’t have a code explanation. I’d like to understand how these two lines of code work in order to understand the solution as a whole.

if (!result.includes(args[i][j])) {
        result.push(args[i][j]);

I added some [spoiler][/spoiler] tags since that is a working solution:

Could someone please explain to me what the following code means?

In terms of JS, that means “for the value args[i][j], if result does not have that value, push that value onto result”. In other words, check if we already have that value in our result and add it if we don’t. We don’t want to add it if we already have it. That is part of the problem to “new array of unique values”. That is testing to make sure they are unique. Without the if check, we would be just pushing the values on whether they were duplicates or not. Basically we would be flattening the array instead of creating a union.

1 Like

Thanks for the clarification.

So in this case, since we let result = empty array, there wouldn’t be any value in the result just yet until we start pushing the unique values into the empty array, is that correct? I guess I’m mostly having trouble understanding which part of the solution code extracts the unique values then pushing them into the empty array.

Yes. 100%. The array starts out as empty. And push is how we add something to the end of an array. It is important to add them to the end because of the “in the order of the original provided arrays” instruction. We are looping through in order so when we push they end up in order.

I guess I’m mostly having trouble understanding which part of the solution code extracts the unique values then pushing them into the empty array.

The code you extracted does it.

In pseudo code:

loop through the arrays
  loop through the values in each array
    if the value is not in the results
      add it to the results

So with this code:

I guess I’m mostly having trouble understanding which part of the solution code extracts the unique values then pushing them into the empty array.

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

gives us:

outer loop: [1, 3, 2]
  inner loop: 1
    is 1 in result? No, add it.
  inner loop: 3
    is 3 in result? No, add it.
  inner loop: 2
    is 2 in result? No, add it.
outer loop: [5, 2, 1, 4]
  inner loop: 5
    is 5 in result? No, add it.
  inner loop: 2
    is 2 in result? Yes, don't add.
  inner loop: 1
    is 1 in result? Yes, don't add.
  inner loop: 4
    is 4 in result? No, add it.
outer loop: [2, 1]
  inner loop: 2
    is 2 in result? Yes, don't add.
  inner loop: 1
    is 1 in result? Yes, don't add.

That’s the logic. Step through it and keep track of what is in result.

1 Like

Thank you very much for your detailed explanation! It makes a lot of sense now from the way you broke it down.

1 Like

Learn how to break it down yourself. That is a valuable skill, to step through code and think in pseudo code.

1 Like

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