ZetCode

Julia Sets Tutorial

last modified November 18, 2026

In Julia, a set is an unordered collection of unique elements. Sets are useful for operations like union, intersection, and difference. This tutorial covers basic definitions and practical examples of using sets in Julia. The examples were run with Julia 1.13. Because sets are unordered, the order in which their elements are printed is an implementation detail and may differ between Julia versions.

Basic Definitions

A set in Julia is created using the Set() function, which takes an iterable such as an array. Sets do not allow duplicate elements, and the order of elements is not preserved.

main.jl
s = Set([1, 2, 3, 4])
println(s)

This creates a set s with elements 1, 2, 3, and 4. Passing a collection with duplicates, for example Set([1, 1, 2]), silently keeps only one copy of each value.

$ julia main.jl
Set([3, 2, 1, 4])

Creating a Set

This example demonstrates how to create a set in Julia.

main.jl
s = Set([1, 2, 3, 4])
println(s)
println(typeof(s))

We create the set from an array of integers and print both the set and its type. The type is Set{Int64}, since Julia infers the element type from the values we passed in.

$ julia main.jl
Set([3, 2, 1, 4])
Set{Int64}

Adding Elements

This example shows how to add elements to a set.

main.jl
s = Set([1, 2, 3, 4])
push!(s, 5)
push!(s, 5)
println(s)

The push! function adds an element. Adding a value that is already present has no effect, so the second push!(s, 5) does not create a duplicate. The exclamation mark is part of the function name and signals that the set is modified in place.

$ julia main.jl
Set([3, 2, 1, 4, 5])

Removing Elements

This example demonstrates how to remove elements from a set.

main.jl
s = Set([1, 2, 3, 4, 5])
pop!(s, 3)
println(s)

The pop!(s, 3) call removes the given element and returns the set. Calling pop!(s) without a value removes and returns an arbitrary element, since the set has no defined order. Removing a value that is not in the set throws an error; use delete! to remove it quietly.

$ julia main.jl
Set([2, 1, 4, 5])

Checking Membership

This example shows how to check if an element is in a set.

main.jl
s = Set([1, 2, 3, 4, 5])
println(2 in s)
println(9 in s)

The in operator returns true when the set contains the value and false otherwise. The unicode forms ∈ and ∉, typed with \in and \notin, are also available.

$ julia main.jl
true
false

Union of Sets

This example demonstrates how to find the union of two sets. The union contains every element that appears in either set.

main.jl
s1 = Set([1, 2, 3])
s2 = Set([3, 4, 5])
println(union(s1, s2))

The union function builds a new set. The value 3 occurs in both sets but appears only once in the result. The equivalent operator is ∪, typed with \cup.

$ julia main.jl
Set([3, 2, 1, 4, 5])

Intersection of Sets

This example shows how to find the intersection of two sets. The intersection contains only the elements shared by both sets.

main.jl
s1 = Set([1, 2, 3])
s2 = Set([3, 4, 5])
println(intersect(s1, s2))

Here only the value 3 is common to both sets. The operator form is ∩, typed with \cap.

$ julia main.jl
Set([3])

Difference of Sets

This example demonstrates how to find the difference between two sets. The difference keeps the elements of the first set that are not in the second one.

main.jl
s1 = Set([1, 2, 3])
s2 = Set([3, 4, 5])
println(setdiff(s1, s2))

The value 3 is dropped because it is present in s2, while 4 and 5 are not added because they are not in s1. The operator \ (setminus) does the same, and is typed with \setminus.

$ julia main.jl
Set([2, 1])

Symmetric Difference

This example shows how to find the symmetric difference of two sets. The symmetric difference contains the elements that are in exactly one of the two sets.

main.jl
s1 = Set([1, 2, 3])
s2 = Set([3, 4, 5])
println(symdiff(s1, s2))

The shared value 3 is excluded, and all the other values from both sets are included. The operator ⊻, typed with \xor, is an alias.

$ julia main.jl
Set([2, 1, 4, 5])

Subset Check

This example demonstrates how to check if one set is a subset of another. A set a is a subset of b when every element of a is also in b.

main.jl
s1 = Set([1, 2, 3])
println(issubset(Set([1, 2]), s1))
println(issubset(Set([1, 9]), s1))

The first call returns true because both 1 and 2 are in s1. The second returns false because 9 is missing. The ⊆ operator, typed with \subseteq, is an alias.

$ julia main.jl
true
false

Set Size

This example shows how to find the number of elements in a set.

main.jl
s1 = Set([1, 2, 3])
println(length(s1))
println(isempty(s1))

The length function returns the number of unique elements. The isempty function reports whether the set has no elements at all.

$ julia main.jl
3
false

Best Practices for Sets

The following tips help when choosing and using sets.

Source

Julia Collections documentation

Julia Sets documentation

In this article, we have explored various examples of using sets in Julia, including basic operations like union, intersection, and difference. For a collection that maps keys to values, see Julia dictionaries; ordered collections are covered in Julia arrays and Julia tuples.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Julia tutorials.