INFO

An Data Structures vs. Abstract Data Types that can store unique values without any particular order. Equivalent to the mathematical Set Theory

  • Uniqueness: No duplicate elements allowed.
  • Unordered: Elements are not stored in any guaranteed order (unless using ordered variants).
  • Membership Testing: Fast lookup to check if an element exists.
  • Dynamic Size: Can grow or shrink as elements are added or removed.

To find the Mathematical Theory and Properties, visit Set Theory


Common Operations

OperationDescriptionTypical Complexity
add(x)Inserts element x into the setO(1) (hash-based)
remove(x)Deletes element x from the setO(1)
contains(x)Checks if x is in the setO(1)
size()Returns the number of elementsO(1)
clear()Removes all elementsO(n)
union(setB)Combines elements from two setsO(n)
intersection(setB)Returns common elements between two setsO(n)
difference(setB)Returns elements in A not in BO(n)

Types of Sets

  • Hash Set: Backed by a Hash Table. Fast operations, no order.
  • Tree Set: Maintains sorted order (e.g., Red-Black Tree).
  • Linked Set: Preserves insertion order.
  • Multiset: Allows duplicates (not a true set, but often grouped here).

Language-Specific Implementations