Welcome to the second module of the course. Having established a framework for algorithmic reasoning, we'll now begin building your toolbox of specific patterns. This module focuses on solving problems efficiently using arrays, strings, and hash-based data structures, which are fundamental in technical interviews.
Today's lesson addresses a foundational problem: detecting duplicates. The main goal is to learn how to use a Set to check for previously seen values in a single pass. As an experienced JavaScript developer, you're certainly familiar with Set, Map, and Object. Our focus here will be to analyze these structures through an algorithmic lens—specifically, how their performance characteristics allow us to write code that is not just correct, but also highly efficient.
The Problem: Contains Duplicate
Let's start with a classic interview problem, often presented as LeetCode #217, "Contains Duplicate":
Given an integer array
nums, returntrueif any value appears at least twice in the array, andfalseif every element is distinct.
For example:
[1, 2, 3, 1]->true(because1appears twice)[1, 2, 3, 4]->false(all elements are unique)[1, 1, 1, 3, 3, 4, 3, 2, 4, 2]->true(multiple duplicates exist)
Before trying to find the best solution, consider the most direct, brute-force approach. How would you check for duplicates using nested loops? Think about the time complexity.
The video below explains this initial thought process.
Contains Duplicate - Leetcode 217 - Python
Watch this short segment from the NeetCode channel on the "Contains Duplicate" problem. It introduces the problem and analyzes the brute-force solution.
Focus on the explanation of the brute-force approach and its O(n^2) time complexity from this section. This establishes the baseline we want to beat.
An solution involves comparing every element with every other element. For an input of size (a common constraint in these problems), this would mean around operations, which is far too slow. The bottleneck is clear: for each element, we are re-scanning the rest of the array to find a match. The core question we're repeatedly asking is, "Have I seen this number before?"
How can we answer that question more efficiently? Instead of searching the input array over and over, we can use a data structure optimized for fast lookups.
The Power of Fast Lookups with Set
In JavaScript, several data structures can store and retrieve data. Let's look at their properties from an algorithmic perspective.

As the table shows, while an array's .includes() method is easy to use, its performance is because it may have to scan the entire array. In contrast, Set, Map, and Object offer , or constant-time, average lookups. This means that, on average, the time it takes to check for an element's existence doesn't depend on the number of items in the collection.
A Set is perfectly suited for our problem for two reasons:
- It stores only unique values.
- It provides a
.has()method that checks for the existence of an element in average time.
This combination allows us to design a new algorithm that processes the array in a single pass.
The "Seen Set" Algorithm:
- Initialize an empty
Set, which we'll callseen. - Iterate through the input array
numsfrom left to right. - For each
numinnums:- Check if
seen.has(num). - If it does, you've found a duplicate. You can immediately return
true. - If it doesn't, add the number to the set:
seen.add(num). This records that you've now encountered this number.
- Check if
- If the loop completes without finding any duplicates, it means all elements were unique. Return
false.
This image illustrates the process. As we iterate through the input [5, 3, 7, 3, 8, 1], we add each new number to the Hash Set. When we encounter the second 3, the set already contains 3, so we've found our duplicate.

The following video provides a clear, dynamic walkthrough of this exact logic.
Contains Duplicate - Leetcode 217 - Python
Watch this segment from the same NeetCode video, which explains the hash set approach.
Pay attention to the animation of adding elements to the hash set and checking for existence. The key part is from this explanation.
Implementation and Complexity
Now, let's turn this logic into code. The following resource provides a clean implementation in TypeScript and analyzes its efficiency.
This Medium article walks through the plan, implementation, and complexity analysis for the hash set approach.
Read the sections on the plan and approach, the code, the explanation, and the complexity analysis. Focus on the core logic and how it translates to the provided code.
As the article explains, this algorithm has:
- Time Complexity: . We iterate through the array of
nelements once. Each set operation (.has()and.add()) is on average. - Space Complexity: . In the worst-case scenario (an array with no duplicates), our
seenset will grow to contain allnelements from the input array.
This is a classic time-space tradeoff. We use extra memory (the set) to reduce our time complexity from a quadratic to a much faster linear .
An Elegant Alternative
Given your deep experience with JavaScript, you'll appreciate that the language often provides concise ways to express common patterns. Since a Set can be constructed directly from an iterable (like an array), we can find a duplicate by comparing the size of the resulting set with the length of the original array.
function containsDuplicate(nums: number[]): boolean {
const uniqueElements = new Set(nums);
return uniqueElements.size < nums.length;
}
This works because new Set(nums) automatically discards all duplicate values. If any duplicates were present in nums, the resulting set's size will be smaller than the original array's length.
This approach is explored in the following video, which contrasts the iterative map/set approach with this more direct one.
Q1. Contains Duplicate | Leetcode 217 | Array questions for Frontend Interview | DSA in Javascript
This video from JsCafe is aimed specifically at JavaScript developers. It first implements the iterative approach (using a Map, which is conceptually similar to our Set approach) and then shows the more concise Set constructor method.
First, watch the walkthrough of the iterative approach from the whiteboard explanation to the code. Then, watch the segment explaining the Set constructor method, which is a very clean and idiomatic JavaScript solution.
Both the iterative method and the Set constructor method have the same time and space complexity, but the latter is often preferred in code golf or when clarity isn't sacrificed. For an interview, being able to explain both is ideal. The iterative "seen set" approach is more fundamental and demonstrates the underlying step-by-step logic, which is a crucial skill.
Key Takeaways
- The "Contains Duplicate" problem is a classic entry point for using hash-based data structures.
- The brute-force solution is too slow for typical constraints. The bottleneck is the repeated linear search to answer "have I seen this before?".
- A
Setprovides average time for both insertions (.add()) and lookups (.has()), making it the ideal tool to optimize this check. - The "seen set" pattern involves iterating through a collection while using a set to keep track of elements encountered so far. This is a fundamental pattern you will use often.
- This pattern improves time complexity to at the cost of auxiliary space—a very common and worthwhile tradeoff in algorithmic problems.
In our next lesson, we will build on this idea. Instead of just checking if we've seen a value before, we'll want to count how many times we've seen it. This will lead us to the "frequency counting" pattern, which typically involves using a Map or a plain Object.
Can't find a good explanation? Sign up and we'll make it for you
Sign up