liveBook · Manning

Set union

As explained in Julia as a Second Language

union is a Julia function and set operation that returns the distinct elements occurring in any of its input collections.

Set union combines collections and returns each distinct element found in at least one input. In Julia, write union(A, B) or use the ∪ operator. For example, union([1, 2, 4], [1, 3]) contains 1, 2, 3, and 4. section 13.3

Union answers an “A or B” question: include elements from the first collection or the second. Sets are designed for these operations and remove duplicates; arrays can also participate, but are less efficient for large collections. section 13.3 section 13.8

Use union when you need to:

  • Combine two groups of records without repeating shared elements. section 13.3
  • Prepare a larger group before applying another set operation such as intersection. section 13.3
  • Choose between readable operator notation, A ∪ B, and the explicit function call, union(A, B). section 13.3

Finding photos from either destination

If Spain and Greece contain photos from two trips, Spain ∪ Greece produces the distinct photos from either destination. To keep only Bob’s photos, combine the operations as Bob ∩ (Spain ∪ Greece), or intersect(Bob, union(Spain, Greece)). section 13.3

S = Bob ∩ (Spain ∪ Greece)
S = intersect(Bob, union(Spain, Greece))

A compact example shows union nested inside intersection, making the “either destination, then Bob” filtering pattern directly runnable. section 13.3

How Julia as a Second Language covers it

How deep it goes
Defines union, compares operator and function forms, and demonstrates composition, but does not examine implementation internals.
Examples
Uses travel-photo collections and numeric sets to demonstrate union results and nested filtering.
What you can do after
Apply it via Julia's union function or ∪ operator to combine collections without duplicate elements.
How it connects
Connects set union with set intersection, set difference, Boolean OR, arrays, membership tests, and relational algebra.

Also explained in

Compare how 4 books explain set union →

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.