Wiki Template Challenge Solution - ashish9342/FreeCodeCamp GitHub Wiki
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
🚩 Remember to use Read-Search-Ask
if you get stuck. Try to pair program 👥 and write your own code 📝
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
Think of apply the function to all arguments, so you Array.prototype.reduce
can be a great help!
try to solve the problem now
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
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
Solution ahead!
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
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
If you found this page useful, you may say thanks to the contributors by copying and pasting the following line in the main chat:
⚠️ 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.