Intermediate Algorithm Scripting : Sorted Union (not removing duplicates)

My code is not removing some duplicates and I cannot find the bug.

  **Your code so far**

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

const newArray = args.reduce((accumulator, currentItem) => {
		return accumulator.concat(
  	currentItem.filter(i => accumulator.indexOf(i) === -1));
}, []);
	
return newArray;

}

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:97.0) Gecko/20100101 Firefox/97.0

Challenge: Sorted Union

Link to the challenge:

With failing tests, my first instinct is to look at the difference between the failing tests and the passing ones. What is unique to the failing test? Hint: Think “duplicates”.

I also like to log things out to see what is happening.

function uniteUnique(arr) {
  const args =  [...arguments];
  console.log('args', args)
  const newArray = args.reduce((accumulator, currentItem) => {
    const filtered = currentItem.filter(i => accumulator.indexOf(i) === -1)
    console.log('\nfiltered', filtered)
    const newAcc = accumulator.concat(filtered);
    console.log('new accumulator', newAcc)
    return newAcc
  }, []); 
  return newArray;
}

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

See if that helps.

1 Like

Thank you Kevin! I appreciate it :slight_smile:

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