Tags

Set

Set

1 lesson
4 problems
4 code snippets

set

Data Structures

1 lesson

Set Basics

Free
Beginner

40 min

1 prereq

Drop a million IDs into a `Set`, ask `set.has(id)` for any one of them, and the answer comes back in expected constant time without any ordering, sorting, or scanning. That single primitive (membership testing) is what makes a **Set** the quiet workhorse behind deduplication, visited-node tracking in graph traversal, and almost every cache-invalidation routine you will read. This lesson covers the uniqueness invariant of a set, the four core operations `add`, `delete`, `has`, and iteration in both JavaScript's `Set` and Python's `set`, and the four classical set-theory operations: union, intersection, difference, and symmetric difference. You will see why a set is implemented as a hash map whose values are unused, and you will practice the visited-set pattern that reappears in BFS, DFS, and cycle detection. In **Hash Map (Dictionary) Basics**, you learned that a hash function maps keys to bucket indices for `O(1)` get and set. A set keeps only the keys, drops the values, and exposes the resulting structure as a pure membership data type. Next, **Trees: Binary Tree Fundamentals** moves from flat unordered collections to hierarchical structures, where parent and child relationships unlock entirely new traversal patterns.

Not Started

0%

Set
Hashing
Data Structures
Beginner
Free
Time Complexity
Visited Set
Comparison

Practice Problems

4 problems

Contains Duplicate

Free
Not Started
Easy

Given an integer array, determine if any value appears at least twice in the array.

Arrays
Set
Beginner

1k

21

Longest Consecutive Sequence

Free
Not Started
Medium

Find the length of the longest consecutive element sequence in an unsorted array in O(n) time using a hash set.

Arrays
Hash Map / Dictionary
Set
Intermediate

1k

24

Valid Sudoku

Not Started
Medium

Determine if a 9x9 Sudoku board is valid by checking rows, columns, and 3x3 sub-boxes for duplicate digits using hash sets.

Arrays
Hash Map / Dictionary
Set
Matrix Algorithms
Intermediate

1k

24

Word Break

Free
Not Started
Medium

Given a string and a dictionary of words, determine if the string can be segmented into a space-separated sequence of dictionary words.

Dynamic Programming
Tabulation
Hash Map / Dictionary
Set
Strings
Algorithms
Intermediate

977

24

Code Snippets

4 snippets
Code Snippet

Get Unique Items from an Array

Deduping an array sounds trivial until objects, NaN, and case-insensitive strings show up. This snippet walks from the one-liner everyone reaches for first, to a key-projecting variant that handles object identity, to the gotcha around NaN that catches even seasoned engineers. Pick the version that matches your data shape and keep the rest as reference.

JavaScript
arrays
utility
set
code-template

468

12

Easy
Code Snippet

Set Intersection and Difference

Computing the items shared between two arrays, the items in the first but not the second, or the symmetric difference is a routine task: comparing API responses, diffing user selections, finding new vs. removed records. This snippet shows the classic `Set`-backed implementations, an object-aware `byKey` variant, and the new native `Set.prototype.intersection` / `difference` methods that landed in Node 22 and 2024 browsers.

JavaScript
arrays
set
utility
sets-logic

757

6

Medium
Code Snippet

Array Equality and Duplicate Detection

Three related questions on arrays: are these two arrays equal in content, does this array have any duplicates at all, and how do I dedupe a list of objects by some key. Each one has a clean linear-time answer once you know which JS collection to lean on (`Set` for primitives, `Map` for keyed objects). The naive `O(n^2)` versions are fine for tiny arrays but show up in code reviews on anything bigger.

JavaScript
arrays
hash-map
set

317

5

Medium
Code Snippet

Generate Unique Random Integers Without Bias

Picking N unique random integers from a range sounds simple, but the right algorithm depends on how N compares to the range size. Retry-into-a-Set is fine when N is small relative to the population, but as N approaches the range size retries dominate and a partial Fisher-Yates shuffle becomes the right answer. For streams of unknown size, reservoir sampling is the only option that gives uniform probability in a single pass.

JavaScript
arrays
set
randomized-algorithms

501

12

Medium