diff --git a/README.md b/README.md index 4a3214633..3f87344cf 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ * [Even or odd number](#even-or-odd-number) * [Factorial](#factorial) * [Fibonacci array generator](#fibonacci-array-generator) +* [Filter out non uniqe values in an array](#filter-out-non-uniqe-values-in-an-array) * [Flatten array](#flatten-array) * [Greatest common divisor (GCD)](#greatest-common-divisor-gcd) * [Head of list](#head-of-list) @@ -118,11 +119,10 @@ var difference = (arr, values) => ### Distance between two points -Use `Math.pow()` and `Math.sqrt()` to calculate the Euclidean distance between two points. +Use `Math.hypot()` to calculate the Euclidean distance between two points. ```js -var distance = x0, y0, x1, y1 => - Math.sqrt(Math.pow(x1-x0, 2) + Math.pow(y1 - y0, 2)) +const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0); ``` ### Escape regular expression @@ -167,6 +167,15 @@ var fibonacci = n => },[]); ``` +### Filter out non-unique values in an array + +Use `Array.filter()` for an array containing only the unique values. + +```js +const unique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i)); +// unique([1,2,2,3,4,4,5]) -> [1,3,5] +``` + ### Flatten array Use recursion. @@ -358,16 +367,12 @@ var tail = arr => arr.slice(1); ### Unique values of array -Use `reduce()` to accumulate all unique values in an array. -Check if each value has already been added, using `includes()` on the accumulator array. + +Use ES6 `Set` and the `...rest` operator to discard all duplicated values. ```js -var uniqueValues = arr => - arr.reduce( (acc, val) => { - if(!acc.includes(val)) - acc.push(val); - return acc; - }, []); +const unique = arr => [...new Set(arr)]; +// unique([1,2,2,3,4,4,5]) -> [1,2,3,4,5] ``` ### URL parameters diff --git a/snippets/distance-between-two-points.md b/snippets/distance-between-two-points.md index e8c91eafd..a93ca2102 100644 --- a/snippets/distance-between-two-points.md +++ b/snippets/distance-between-two-points.md @@ -1,8 +1,7 @@ ### Distance between two points -Use `Math.pow()` and `Math.sqrt()` to calculate the Euclidean distance between two points. +Use `Math.hypot()` to calculate the Euclidean distance between two points. ```js -var distance = x0, y0, x1, y1 => - Math.sqrt(Math.pow(x1-x0, 2) + Math.pow(y1 - y0, 2)) +const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0); ``` diff --git a/snippets/filter-out-non-uniqe-values-in-an-array.md b/snippets/filter-out-non-uniqe-values-in-an-array.md new file mode 100644 index 000000000..622026234 --- /dev/null +++ b/snippets/filter-out-non-uniqe-values-in-an-array.md @@ -0,0 +1,8 @@ +### Filter out non-unique values in an array + +Use `Array.filter()` for an array containing only the unique values. + +```js +const unique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i)); +// unique([1,2,2,3,4,4,5]) -> [1,3,5] +``` diff --git a/snippets/unique-values-of-array.md b/snippets/unique-values-of-array.md index d00c7f112..0d7639815 100644 --- a/snippets/unique-values-of-array.md +++ b/snippets/unique-values-of-array.md @@ -1,13 +1,8 @@ ### Unique values of array -Use `reduce()` to accumulate all unique values in an array. -Check if each value has already been added, using `indexOf()` on the accumulator array. +Use ES6 `Set` and the `...rest` operator to discard all duplicated values. ```js -var uniqueValues = arr => - arr.reduce( (acc, val) => { - if(acc.indexOf(val) === -1) - acc.push(val); - return acc; - }, []); +const unique = arr => [...new Set(arr)]; +// unique([1,2,2,3,4,4,5]) -> [1,2,3,4,5] ```