Julia Sets
A Set is an unordered collection of unique values. Adding a value that's already present does nothing. Sets answer "is this value in the collection?" very quickly and support the operations you know from mathematics: union, intersection and difference.
colors = Set(["red", "green", "red", "blue", "green"])
println(length(colors))
println("red" in colors, " ", "pink" in colors)
push!(colors, "pink")
push!(colors, "red") # already there: no change
println(length(colors))
delete!(colors, "green")
println(sort(collect(colors)))
println(typeof(colors))3
true false
4
["blue", "pink", "red"]
Set{String}
Like dictionaries, sets have no defined order, so this lesson sorts them before printing.
Removing duplicates
Converting to a Set is a quick way to find distinct values. If you want to keep the original order, unique is the better tool:
visits = ["home", "about", "home", "blog", "about", "home"]
println(length(Set(visits)), " distinct pages")
println(unique(visits))
println(allunique([1, 2, 3]), " ", allunique(visits))3 distinct pages
["home", "about", "blog"]
true false
Set operations
| Operation | Function | Unicode operator |
|---|---|---|
| union | union(a, b) |
a ∪ b |
| intersection | intersect(a, b) |
a ∩ b |
| difference | setdiff(a, b) |
|
| symmetric difference | symdiff(a, b) |
|
| subset test | issubset(a, b) |
a ⊆ b |
| membership | in(x, a) |
x ∈ a, x ∉ a |
python_devs = Set(["ana", "ben", "cara", "dev"])
julia_devs = Set(["cara", "dev", "eli"])
println(sort(collect(python_devs ∪ julia_devs)))
println(sort(collect(python_devs ∩ julia_devs)))
println(sort(collect(setdiff(python_devs, julia_devs))))
println(sort(collect(symdiff(python_devs, julia_devs))))
println(Set(["cara"]) ⊆ julia_devs, " ", "ben" ∉ julia_devs)["ana", "ben", "cara", "dev", "eli"]
["cara", "dev"]
["ana", "ben"]
["ana", "ben", "eli"]
true true
These functions also work on arrays, returning arrays and keeping first-seen order:
println(union([1, 2, 2, 3], [3, 4]))
println(intersect([5, 1, 3], [3, 5, 7]))
println(setdiff(1:6, [2, 4]))[1, 2, 3, 4]
[5, 3]
[1, 3, 5, 6]
In-place versions
union!, intersect! and setdiff! modify the first set instead of creating a new one:
seen = Set{Int}()
for n in [4, 8, 4, 15, 8, 16]
if n in seen
println("duplicate: ", n)
else
push!(seen, n)
end
end
union!(seen, [23, 42])
println(sort(collect(seen)))duplicate: 4
duplicate: 8
[4, 8, 15, 16, 23, 42]
Set{Int}() creates an empty set with a fixed element type, so the compiler knows exactly what it holds.
When to use a Set
- Membership tests on large collections:
x in sis fast for aSetbut scans every element of aVector. - Deduplication where order doesn't matter.
- Comparing groups: which tags two posts share, which students are missing an assignment, and so on.
Elements must be hashable and shouldn't be mutated after insertion (for example, don't put an array into a set and then change it).
Test yourself
What is length(Set([1, 1, 2, 3, 3]))?
- 5
- 3
- 2
- 1
Which function returns the elements in a but not in b?
- intersect(a, b)
- union(a, b)
- setdiff(a, b)
- symdiff(a, b)
Which function removes duplicates from a vector while keeping the original order?
- Set
- unique
- sort
- distinct