Sorted union , stuck

Hello all,

edit: my most recent code is the last post.

I have not tried submitting this yet, I’m just testing it out on my laptop so far.
but it just seems to return the entire array?
I think the find should determine if a number is already in array and then return true, preventing it pushed again?
what am I missing? Thanks :slight_smile:

edit: it works better with the recently two added lines to create a single array for the input first.
It returns the correct result except for some added commas ?
I can’t figure out how to get rid of these now :slight_smile:

Your code so far


function uniteUnique(...arr) {
        let array=[];
	let tempArray =[arr];
	let iterator = tempArray.values();
      //these two lines are to create a single array 
       let str = tempArray.join("");
	tempArray = str.split("");
	
	let results = tempArray.filter((item)=>{
		
	    let result = array.includes(iterator.next().value);
		if(!result){
		   array.push(iterator.value);
		   return array;
		}
		
	});
	return results;
}

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; rv:69.0) Gecko/20100101 Firefox/69.0.

Challenge: Sorted Union

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

The error i am getting is:

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

should return

[1, 3, 2, 5, 4]

.

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

should return

[1, 2, 3, 5]

.

uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8])

should return

[1, 2, 3, 5, 4, 6, 7, 8]

.

are you sure your filter method is used appropriately? I suggest reading on it, and seeing why it doesn’t filter

I finally figured out I need to use map as well with join and split with 2d arrays, not just join and split.
So this is my code , it returns the correct results on my laptop , yet it still won’t pass here on FCC ? Can anyone offer some insight ? I’d rather not waste too much more time on this . Thanks :slight_smile:

So my code now:

       function uniteUnique(...arr) {                           
	let array=[];
	let tempArray =[...arr];    
	let str =tempArray.map(item => item.join("")).join("");
	tempArray= str.split("");
	
	const results = tempArray.filter((item)=>{
	    let result = array.includes(item);                
		if(!result){
		   array.push(item);                              
		   return array;
		}
	});
	return results;
}

Couple hints for you:

/* How to make array flat */
const array = [[1, 2, 3], [3, 4, 5], [5, 6, 7]];
console.log(array.flat());
/* How to quickly remove duplicates from array */
const array = [1, 1, 1, 1, 42, 1, 1, 1];
console.log([...new Set(array)]);

Thank you for that . So it works with just these lines , but I still don’t understand why my code wasn’t passing, even though it seems to return the same results ?
:slight_smile:

As ieahleen said. Had to do with the filter method.
Had you checked and Google it like example:


Then you might have figured out what was wrong.

But the assignment passed when I used flat() (instead of join() and split()) , and my filter code ?
I tried that as I wanted to know if it would be deemed correct.

On my laptop, just the two other lines from snigo, worked as well :slight_smile: