Sets as an ADT

A set is an unordered collection of unique elements. Add, remove, test membership, then combine sets with union, intersection, and difference.

Data structures Beginner+ ADT · Uniqueness ~8 min
The idea

Unordered and unique

Unlike a list, a set does not care about order and never stores duplicates. Asking “is x in the set?” is the core question — membership.

In Python, set is built on a hash table (same family as dict). That is why membership and insert are typically fast.

TipAdding an element that is already present is a no-op — uniqueness is automatic.
IdeaPython set
set_basics.pyPython
s = set()
s.add(3)
s.add(8)
s.add(3)      # ignored — already there
8 in s         # True
s.discard(3)

Chips float in a bag — order on screen is not meaningful.

Interactive

Run the simulation

Switch modes: basics, set algebra, and subset relationships.

Step 0 / 0
demo.pyPython
Set viewdemo
Ready
Shortcuts: Space play/pause · → step · ← back · R reset
Try it yourself

Build a set

Add, remove, or check membership. Duplicates are ignored.

Ready
Set is empty. Enter a value and press Add.
Remember

Set mental model

Unique elements, membership first, then algebra.

Unique

No duplicates

Adding an existing element leaves the set unchanged.

Ops

Union · ∩ · Difference

| / & / - in Python build new sets from two inputs.

Engine

Hash table under the hood

Python set / Java HashSet store elements by hash — see the hash table sim next.

Up next

Hash tables

See how hashing, collisions, and load factor power sets and dictionaries.

Open hash tables