Sorted Union using set?

Sorted Union using set?
0

#1

Aaahhh it’s always the last step!

I first made a set of unique values for reference --> singles

I’m trying to make a new array with map, from the original array from the arguments
then, filter is matching the items in the original array against the set.

Then I’m trying to have the set delete each unique value as it gets matched, this should return true to filter, which then keeps that value in the new original array and proceeds forwards, right??

I just get the error that singles.delete is not a function?

Your code so far


function uniteUnique(arr) {
	let args = arr.slice.call(arguments);
	let merged = [].concat(...args);

	let singles = [...new Set(merged)];					

	let findUnique = args.map(x => {
		x.filter((item) => {
		singles.delete(item);										
		}) 
})
console.log(findUnique);
	
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union


#2

A Set is a collection of unique items, it doesn’t have duplicates: you can’t delete duplicates from a set because they don’t exist in the first place — in your code, singles only has one of every value.

Also you aren’t returning anything from the function


#3

I know, I’m not trying to delete the duplicates in the set. Essentially I want my function to work like:
Set --> uniqueValues [1,3,2,5,4]
endResult = 2d array [ [ 1, 3, 2 ], [ 5, 2, 1, 4 ], [ 2, 1 ] ]

I want my function to iterate over each array in endResult to find a match with the Set. Once it does that, then delete that value from the set, pass true to .filter() so it keeps it in endResult and then continue until the set is empty -> all matches have been made.

Sorry if I was a bit unclear, I’m having trouble telling myself what I’m doing!
It seems a bit convoluted now that I think about it. But it should still be possible.

Also, yes, I’ve just been console logging instead of returning my answer. It shouldn’t make a difference though.


#4

I can’t believe it. The answer was staring at me the whole time. And I went around it and made everything waaaay more complicated.

I solved it with 4 lines of code :man_facepalming:

function uniteUnique(arr) {
	let args = arr.slice.call(arguments);
	let uniques = [...new Set([].concat(...args))];					
return(uniques);	
}
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);

I hope this doesn’t happen too often…