From ba33e9e1defd13cb0fb7b94d2118b6258cb00142 Mon Sep 17 00:00:00 2001 From: Alhassan Atama Date: Mon, 19 Oct 2020 17:47:26 +0100 Subject: [PATCH 1/2] Added allUnique snippet --- snippets/allUnique.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 snippets/allUnique.md diff --git a/snippets/allUnique.md b/snippets/allUnique.md new file mode 100644 index 000000000..f00a8e281 --- /dev/null +++ b/snippets/allUnique.md @@ -0,0 +1,19 @@ +--- +title: allUnique +tags: array,beginner +--- + +Checks if all elements in an array are equal and returns`true` else returns `false`. + +- Use `Array.prototype.length` property to return the number of elements in that array. +- Use `Set()` object to collect unique elements from the given array and then the `Set.prototype.size` to get the number of elements in the set +- Check the two values for equality + +```js +const allUnique = myArray => myArray.length === new Set(myArray).size; +``` + +```js +allUnique([1,2,3,4,5]); // true +allUnique([1,1,2,3,4]); // false +``` From a727c1eaea2ff6d5838a9f51ac9e3d3c29485e0d Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Mon, 19 Oct 2020 22:17:39 +0300 Subject: [PATCH 2/2] Update allUnique.md --- snippets/allUnique.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/snippets/allUnique.md b/snippets/allUnique.md index f00a8e281..bde0d3dc9 100644 --- a/snippets/allUnique.md +++ b/snippets/allUnique.md @@ -3,17 +3,16 @@ title: allUnique tags: array,beginner --- -Checks if all elements in an array are equal and returns`true` else returns `false`. +Checks if all elements in an array are unique. -- Use `Array.prototype.length` property to return the number of elements in that array. -- Use `Set()` object to collect unique elements from the given array and then the `Set.prototype.size` to get the number of elements in the set -- Check the two values for equality +- Create a new `Set` from the mapped values to keep only unique occurences. +- Use `Array.prototype.length` and `Set.prototype.size` to compare the length of the unique values to the original array. ```js -const allUnique = myArray => myArray.length === new Set(myArray).size; +const allUnique = arr => arr.length === new Set(arr).size; ``` ```js -allUnique([1,2,3,4,5]); // true -allUnique([1,1,2,3,4]); // false +allUnique([1, 2, 3, 4]); // true +allUnique([1, 1, 2, 3]); // false ```