From 5b884403a15139645363d95c970ce1a6d875217a Mon Sep 17 00:00:00 2001 From: jun-low Date: Sun, 4 Oct 2020 20:45:05 +0800 Subject: [PATCH] Create isDuplicates.md --- snippets/isDuplicates.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 snippets/isDuplicates.md diff --git a/snippets/isDuplicates.md b/snippets/isDuplicates.md new file mode 100644 index 000000000..0a74f55eb --- /dev/null +++ b/snippets/isDuplicates.md @@ -0,0 +1,22 @@ +--- +title: isDuplicates +tags: array,intermediate +--- + +Check if there's duplicate values in a flat array. + +- Convert original array into a Set. +- The `.size()` returns the number of (unique) elements in a Set object. +- Compare the lengths of the array and the Set. + + +```js +function isDuplicates(array) { + return new Set(array).size !== array.length +} +``` + +```js +isDuplicates([0,1,1,2]); // 'True' +isDuplicates([0,1,2,3,]); // 'False' +```