Skip to main content
Engineering LibreTexts

10: Sets

  • Page ID
    46708
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Sets are helpful tools in a software application where, just as in mathematics, similar abstract objects are aggregated into collections. A mathematical set has a fairly simple interface and can be implemented in a surprising number of ways.

    Set<item-type> ADT

    contains(test-item:item-type):Boolean

    True if test-item is contained in the Set.

    insert(new-item:item-type)

    Adds new-item into the set.

    remove(item:item-type)

    Removes item from the set. If item wasn't in the set, this method does nothing.

    remove(item-iter:List Iterator<item-type>)

    Removes the item referred to by item-iter from the set.

    get-begin():List Iterator<item-type>

    Allows iteration over the elements in the Set.

    get-end():List Iterator<item-type>

    Also allows iteration over the elements in the Set.

    union(other:Set<item-type>):Set<item-type>

    Returns a set containing all elements in either this or the other set. Has a default implementation.

    intersect(other:Set<item-type>):Set<item-type>

    Returns a set containing all elements in both this and the other set. Has a default implementation.

    subtract(other:Set<item-type>):Set<item-type>

    Returns a set containing all elements in this but not the other set. Has a default implementation.

    is-empty():Boolean

    True if no more items can be popped and there is no top item.

    get-size():Integer

    Returns the number of elements in the set.

    All operations can be performed in \(O(N)\) time.


    This page titled 10: Sets is shared under a CC BY-SA license and was authored, remixed, and/or curated by Wikibooks - Data Structures (Wikipedia) .

    • Was this article helpful?