Wiki Template Challenge Solution - ashish9342/FreeCodeCamp GitHub Wiki

Algorithm Article Template

This is the template that you should follow when writing new algorithm solutions or adding content, always make sure the latest version of this template is implemented on your pull request

Algorithm Find the Symmetric Difference

🚩 Remember to use Read-Search-Ask if you get stuck. Try to pair program 👥 and write your own code 📝

🏁 Problem Explanation:

Symmetric difference is the difference between two sets i.e., the collection of elements which are members of either set but not both.

In the symmetric difference algorithm, you would work through the arrays of numbers in this manner: sym(A, B, C) translates to sym(sym(A, B), C) i.e., the symmetric difference of set A and set B is found first and then, the symmetric difference of the resultant set and set C is found.

Example: sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) equals [1, 4, 5].

For more explanation please view the original post at https://www.freecodecamp.org/learn/coding-interview-prep/algorithms/find-the-symmetric-difference

Relevant Links

💬 Hint: 1

Think of apply the function to all arguments, so you Array.prototype.reduce can be a great help!

try to solve the problem now

💬 Hint: 2

Your function must not consider a number that belongs to more than more arrays. So you should think about uniqueness. You can use Array.prototype.filter to achieve this

try to solve the problem now

💬 Hint: 3

Finally, the resulting array must contain only unique values (no duplicates). So you can use Set Data Structure to guarantee uniqueness.

try to solve the problem now

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

:advanced: Advanced Code Solution:

const sym = (...args) => {
  // Merge all the different arrays and remove duplicate elements it means elements that are present both on two related arrays
  let tab = args.reduce((a, b) => [
    ...a.filter(i => !b.includes(i)),
    ...b.filter(j => !a.includes(j))
  ], []);
  // Then remove the rest of duplicated values and sort the obtained array
  return Array.from(new Set(tab)).sort((a, b) => a - b);
}

🚀 Run Code

Code Explanation:

First, I use reduce to loop over all the arrays and also to end up with one value. Then for each loop, I tried to get the difference between the previousArray and the currentArray by using filter which will remove all elements that belongs to both of them.

Finally, As I said on hint, I use Set to remove to duplicated values since its guarantee with Set and then I transform back to Array using Array.form so that I can easily sort the obtained array to finally end up with the answer

🏆 Credits:

If you found this page useful, you may say thanks to the contributors by copying and pasting the following line in the main chat:

📋 NOTES FOR CONTRIBUTIONS:

  • ⚠️ DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
  • Add an explanation of your solution.
  • Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. 🚥
  • Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)

See 👉 Wiki Challenge Solution Template for reference.

⚠️ **GitHub.com Fallback** ⚠️