Intermediate Algorithm Scripting: Sorted Union

Intermediate Algorithm Scripting: Sorted Union
0

#1

Here’s my code (not yet completed). I understand the reason my filter is not working (the === -1) doesn’t make sense since it’s not checking for “duplicates”, more so, just the existing of that item in the existing array, which will obviously not be -1 since it would just find itself. I’m confused how the solution doesn’t have this issue though, since it seems like its following the same logic with the filter, I believe.

My code:

function uniteUnique(arr) {
let newArr = Array.from(arguments)
let singleArr = newArr.reduce(function(a,b){
  return a.concat(b);
})

let ans = singleArr.filter(function(a){
   singleArr.indexOf(a) === -1
  console.log(singleArr.indexOf(a) + ' a:' + a)
})

console.log(ans);

}

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

Solution

function uniteUnique(arr1, arr2, arr3) {
 var newArr;
 //Convert the arguments object into an array
  var args = Array.prototype.slice.call(arguments);
  //Use reduce function to flatten the array
  newArr = args.reduce(function(arrA,arrB){
  //Apply filter to remove the duplicate elements in the array
    return arrA.concat(arrB.filter(function(i){
      return arrA.indexOf(i) === -1;
    }));
  });

   return newArr;                    
}

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

#2

Your logic is a bit different than theirs.

It might not be readily apparent but in that solution the first array is compared against each element in the following arrays. If one of those elements is not is not present it gets concatenated onto the first array.

Tracing the example function call uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
[ 1, 3, 2 ] [ 5, 2, 1, 4 ] first array compared to second, concatenating unique elements to make [ 1, 3, 2, 5, 4 ]
[ 1, 3, 2, 5, 4 ] [ 2, 1 ] new first array compared to third, concatenating unique elements to make [ 1, 3, 2, 5, 4 ]
[ 1, 3, 2, 5, 4 ] resultant array all unique elements in order they appear.

In your solution you pack all elements from all arrays into one big array. Then you are testing each element to see if it is NOT present in that array. Obviously every element is present in the array where it resides so no elements ‘pass the test’ and so no elements go into your resultant array. (Filter creates a new array, it does nothing to the array you are filtering).

let ans = singleArr.filter(function(a){
   singleArr.indexOf(a) === -1 //this will always be false, a is a member of singleArr
  console.log("a:", a, singleArr, singleArr.indexOf(a))
})

Change your log statement as above and you will see why this is not working.

This can also be solved with a simple nested for loop that is probably much clearer than that ‘solution’.


#3

@dkang17 Please do not post full working solutions in the future. Instead, just reference which Guide solution to which you are referring… Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution.

Thank you.