From 7dd2fe19f3eeb7162d2d039222fce17530d26017 Mon Sep 17 00:00:00 2001 From: Nikolai Kiselev Date: Fri, 19 Jul 2019 07:07:09 +0700 Subject: [PATCH] Update a solution for "Find the Symmetric Difference" algorithm (#36379) * Update advanced solution * Replace HTML links with markdown syntax * Remove obsolete sort() * Use spread operator instead of Array.from --- .../find-the-symmetric-difference/index.md | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/guide/english/certifications/coding-interview-prep/algorithms/find-the-symmetric-difference/index.md b/guide/english/certifications/coding-interview-prep/algorithms/find-the-symmetric-difference/index.md index 0fbee075dd..2b215ad46e 100644 --- a/guide/english/certifications/coding-interview-prep/algorithms/find-the-symmetric-difference/index.md +++ b/guide/english/certifications/coding-interview-prep/algorithms/find-the-symmetric-difference/index.md @@ -14,10 +14,9 @@ Following above definition, symmetric difference of three sets *A*, *B*, and *C* #### Relevant Links #### -* Symmetric difference - Wikipedia -* Symmetric difference - YouTube -* JavaScript Array.prototype.reduce() +* [Symmetric difference - Wikipedia](https://en.wikipedia.org/wiki/Symmetric_difference) +* [Symmetric difference - YouTube](https://www.youtube.com/watch?v=PxffSUQRkG4) +* [Array.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) ## ![:speech_balloon:](https://forum.freecodecamp.com/images/emoji/emoji_one/speech_balloon.png?v=3 ":speech_balloon:") Hint: 1 ## @@ -96,11 +95,11 @@ In the event of *odd number of sets* the symmetric difference will include ident #### Relevant Links #### -* JavaScript for -* JavaScript Array.length -* JavaScript Array.prototype.push() -* JavaScript Array.prototype.forEach() -* JavaScript Array.prototype.indexOf() +* [Statement for](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/statements/for) +* [Array.length](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/length) +* [Array.push()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) +* [Array.forEach()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) +* [Array.indexOf()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) ## ![:sunflower:](https://forum.freecodecamp.com/images/emoji/emoji_one/sunflower.png?v=3 ":sunflower:") Intermediate Code Solution: ## @@ -151,23 +150,21 @@ In the event of *odd number of sets* the symmetric difference will include ident #### Relevant Links #### -* JavaScript Array.prototype.slice() -* JavaScript Array.prototype.filter() -* JavaScript Array.prototype.concat() +* [Array.slice()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) +* [Array.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) +* [Array.concat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat) ## ![:rotating_light:](https://forum.freecodecamp.com/images/emoji/emoji_one/rotating_light.png?v=3 ":rotating_light:") Advanced Code Solution: ## ```javascript - function sym() { - let argv = Array.from(arguments).reduce(diffArray); - return argv.filter((element, index, array) => index === array.indexOf(element));//remove duplicates - } + const diff = (arr1, arr2) => ( + [ + ...arr1.filter(e => !arr2.includes(e)), + ...arr2.filter(e => !arr1.includes(e)), + ] + ); - function diffArray(arr1, arr2) { - return arr1 - .filter(element => !arr2.includes(element)) - .concat(arr2.filter(element => !arr1.includes(element))); - } + const sym = (...args) => [...new Set(args.reduce(diff))]; // test here sym([1, 2, 3], [5, 2, 1, 4]); @@ -176,14 +173,13 @@ In the event of *odd number of sets* the symmetric difference will include ident ### Code Explanation: ### -* The main function *sym()* creates an array from *arguments* and reduces its elements utilising helper function *diffArray()* to a single array. +* The main function *sym()* reduces given arrays utilising helper function *diff()* to a single array. Also, it temporary converts the result to *Set* to remove duplicates. -* The function *diffArray()* returns the symmetric difference of two arrays by picking out unique elements in parameterised arrays; *arr1* and *arr2*. +* The function *diff()* returns the symmetric difference of two arrays by picking out elements in parameterised arrays; *arr1* and *arr2*. #### Relevant Links #### - -* JavaScript Array.from() -* JavaScript Array.prototype.filter() +* [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) +* [Array.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) ## ![:clipboard:](https://forum.freecodecamp.com/images/emoji/emoji_one/clipboard.png?v=3 ":clipboard:") NOTES FOR CONTRIBUTIONS: ##