DEV Community

cortim
cortim

Posted on

Quick Tip: Find if array has duplicates

Given the array below, which contains duplicates:
const arr = [1, 2, 5, 2, 6, 1];

We can perform the following check:
const hasDuplicates = (new Set(arr)).size !== arr.length;

The result is true, indicating the presence of duplicates in the array.

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❀ or a friendly comment on this post if you found it helpful!

Okay