Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 61 / 68

Set operations with Set (union, intersection, difference)

This lesson covers unions, intersections, and differences with Julia's Set, with the aim of understanding the basics of set operations.

The union(), intersect(), and setdiff() functions calculate the union, intersection, and difference of sets directly.

The sample code applies each function to Set([1, 2, 3, 4]) and Set([3, 4, 5, 6]), converts the result with collect(), and orders it readably with sort().

A common early stumble is that displaying a Set directly gives no guaranteed order. To fix the display order you combine collect() and sort(), as the sample does.

It is a useful way of thinking for real data work — finding the data common to two groups — and intersections and differences also help in understanding SQL joins.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)