liveBook · Manning

Set intersection

As explained in Julia as a Second Language

Set intersection is the operation that produces the elements shared by two or more sets.

Set intersection produces a new set containing only the elements shared by two or more sets. In Julia, write it as ∩ or call intersect; it acts like logical AND because an element must belong to every intersected set. section 13.3

Use intersection when a result must satisfy multiple membership conditions:

  • Find pictures of Bob taken in either Spain or Greece with Bob ∩ (Spain ∪ Greece). section 13.3
  • Find items shared by two sets with intersect(A, B) or A ∩ B. section 13.3
  • Combine several criteria by intersecting intermediate results, such as (bob ∪ eve) ∩ joe. section 13.3

Finding shared values

With A = Set([1, 2, 4, 6]) and B = Set([1, 3, 5, 6]), A ∩ B returns the set containing 1 and 6. The function form, intersect(A, B), returns the same result. For caption sets, bob ∩ joe finds "Joe and Bob in Florida".

Figure 13.2 Venn diagrams helping explain set operations
13-02

A Venn diagram makes the result intuitive: shade only the overlapping region of the sets. section 13.3

How Julia as a Second Language covers it

How deep it goes
Explains the definition, Boolean analogy, Julia syntax, Venn-diagram intuition, and combined examples, but not implementation internals.
Examples
Demonstrates shared photo captions, numeric sets, combined people searches, Venn diagrams, and product-criteria matching.
What it assumes
Assumes basic collections and membership, while clarifying how sets differ from arrays and how set operations work.
What you can do after
Apply it via Julia's ∩ operator or intersect function to retrieve values shared across sets.
How it connects
Connects set intersection with Boolean logic, union, set difference, arrays, relational databases, and SQL inner joins.

Also explained in

Compare how 5 books explain set intersection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.