From 9ffc7b47117e95f6eaedb9e15d6cc0c037116770 Mon Sep 17 00:00:00 2001 From: Vaibhav <49142340+vaibhavgope@users.noreply.github.com> Date: Wed, 2 Feb 2022 23:58:31 +0530 Subject: [PATCH] fix(curriculum): add two edge test cases to the sorted union problem (#44860) * fix: fixed solution of sorted union * fix: improved solution of sorted union problem --- .../sorted-union.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union.md index cbc9cd794a..5090c75865 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union.md @@ -45,6 +45,18 @@ assert.deepEqual(uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]), [ ]); ``` +`uniteUnique([1, 3, 2], [5, 4], [5, 6])` should return `[1, 3, 2, 5, 4, 6]`. + +```js +assert.deepEqual(uniteUnique([1, 3, 2], [5, 4], [5, 6]), [1, 3, 2, 5, 4, 6]); +``` + +`uniteUnique([1, 3, 2, 3], [5, 2, 1, 4], [2, 1])` should return `[1, 3, 2, 5, 4]`. + +```js +assert.deepEqual(uniteUnique([1, 3, 2, 3], [5, 2, 1, 4], [2, 1]), [1, 3, 2, 5, 4]); +``` + # --seed-- ## --seed-contents-- @@ -62,7 +74,11 @@ uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]); ```js function uniteUnique(arr) { return [].slice.call(arguments).reduce(function(a, b) { - return [].concat(a, b.filter(function(e) {return a.indexOf(e) === -1;})); + return [].concat( + a, + b.filter(function(e, currentIndex) { + return b.indexOf(e) === currentIndex && a.indexOf(e) === -1; + })); }, []); } ```