DEV Community

DR
DR

Posted on

2

#217: Contains Duplicate

Honestly, this was the easiest problem I've come across so far on LeetCode. The directives were as follows:

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

Immediately I started thinking about using one of JS's built in data types, specifically Set(). Why? Because a set doesn't contain duplicates, so instead of iterating the traditional way, we can just convert the array to a set, back to an array, and compare the lengths to see if there were any duplicates that the set eliminated.

Let's take a look at first converting the array to a set.

function containsDuplicate(nums) {
  return new Set(nums);
}
Enter fullscreen mode Exit fullscreen mode

Nice - remember that we need to use the new keyword and not just Set(). Now, the value stored in that is the list with all duplicates eliminated. Let's convert that back to an array so we can compare more easily.

function containsDuplicate(nums) {
  return [...new Set(nums)];
}
Enter fullscreen mode Exit fullscreen mode

I'm using the spread operator because it looks cleaner, but you could also use Array.from() or any other method. Now, let's look at the final step - comparing the lengths.

why compare the lengths? It's simple - because if there were any duplicates, the set will have deleted them from our new array so the lengths will be different. Therefore, we'll return if they aren't different so we correctly handle the booleans (true if there were duplicates, false if there weren't).

function containsDuplicate(nums) {
  return [...new Set(nums)].length != nums.length;
}
Enter fullscreen mode Exit fullscreen mode

So there you go. An elegant one-liner that does the job pretty efficiently.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

nextjs tutorial video

đź“ş Youtube Tutorial Series

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series đź‘€

Watch the Youtube series

đź‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay