Stretched myself on the diffArray intermediate algorithm challenge

Hello everyone, I’m still new to programming and I’m very excited right now about what I just did and I think it works fine.

On the diffArray challenge in the Intermediate Algorithm Scripting section, I first figure out the main challenge i.e writing an algorithm that compares two arrays passed as args to a function and returns an array of their difference .

Then I thought to myself what if I could write an algorithm that compares more than two arrays, preferably as many arrays as you pass into the function, and return an array of their difference. And yes, i did it!

Here’s my solution. (Please make it better if you can)

function diffOfArrays(...arg) {
  var combinedArr = [];
  var newArr = [];

  arg.map(item => {
    if (item instanceof Array) {
      combinedArr.push(...item);
    }
  });

  for (let item of combinedArr) {
    if (combinedArr.lastIndexOf(item) === combinedArr.indexOf(item)) {
      newArr.push(item);
    }
  }
  return newArr;
}
1 Like

Your solution looks good, taking a challenge and playing with it and changing it is a great way to learn.

The only part I’d change is this:

  arg.map(item => {
    if (item instanceof Array) {
      combinedArr.push(...item);
    }
  });

You’re misusing .map here a little, .map is meant to take an array, transform each item into something different, and return the changed array. You’re using it here as a replacement for a for loop, so the Array method you’d pick for that is .forEach.

You can also avoid a loop altogether if you use the spread operator and .flat to create the combined array (optionally filter first if the arguments are indeed arrays)

combinedArr = [...arg].filter(arr => Array.isArray(arr)).flat();
1 Like

You just made me a better programmer. First time I’m getting to know the .flat() method. Thanks a lot for your time.

combinedArr = args.flat(infinity);
//args is already an array

//using filter here is not needed 

//I used infinity as my depth argument as I assume
//arrays can be arbitrarily nested 

I went to make my own solution and got a bit carried away with compatibility so it is probably not as efficient as I could have made it, but it might do alright for code golf

const diffOfArrays = (...args) => [...(new Set(args.flat(Infinity)))];
console.log(diffOfArrays([1,1,[3,3,[4,4]]])) //[1, 3, 4]

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

1 Like

The .filter() may be needed to make sure only array args are compared. So it’s optional.

Well, in my own case I wanted the function to compare more than two array args, preferably variable number of array args. So there should be at least two array args, irrespective of the depth of any one.