Menu

gitpiper

hasDuplicates javascript Code Snippet in 2024

arraybeginner

Last Updated: 22 July 2024

Checks if there are duplicate values in a flat array.

  • Use Set() to get the unique values in the array.
  • Use Set.prototype.size and Array.prototype.length to check if the count of the unique values is the same as elements in the original array.
const hasDuplicates = arr => new Set(arr).size !== arr.length;
hasDuplicates([0, 1, 1, 2]); // true hasDuplicates([0, 1, 2, 3]); // false

javascript snippet similar to hasDuplicates For You in July 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️