Set ADT Simulator
A Set stores unique elements. Membership is more important than position: duplicates are rejected, order is not part of the mathematical abstraction, and core operations include union, intersection, difference, and subset tests.
AddAverage O(1)
HasAverage O(1)
DeleteAverage O(1)
DuplicatesNot allowed
Set operations
Set A
Set B
Example sets loaded.
—
|A|0
|B|0
|A ∪ B|0
|A ∩ B|0
Set concepts
Uniqueness
Adding an element already present in a set does not create a duplicate and does not change the set size.
Membership
Set algorithms usually ask whether an element belongs to a set rather than where that element is stored.
Mathematical operations
Union combines elements, intersection keeps common elements, and difference keeps elements present in one set but not the other.
Implementation note
Java HashSet is backed by hashing and offers average constant-time membership operations; TreeSet keeps elements ordered with logarithmic operations.
Set operation summary
| Operation | Meaning |
|---|---|
| A ∪ B | Elements in A or B or both. |
| A ∩ B | Elements common to A and B. |
| A − B | Elements in A but not in B. |
| A △ B | Elements in exactly one of the two sets. |
| A ⊆ B | Every element of A is also in B. |
| A = B | A and B contain exactly the same elements. |