Array elements are ordered, but can contain duplicates, that is, the same value can occur at different indices. In a dictionary, keys have to be unique, but the values do not, and the keys are not ordered. If you want a collection where order does not matter, but where the elements have to be unique, then use a Set. Creating a set is as easy as this:
// code in Chapter 5\sets.jl: s = Set([11, 14, 13, 7, 14, 11])
The Set()
function creates an empty set Set(Any[])
. The preceding line returns Set([7, 14, 13, 11])
, where the duplicates have been eliminated.
Operations from the set theory are also defined for s1 = Set([11, 25])
and s2 = Set([25, 3.14])
as follows:
union(s1, s2)
producesSet([3.14,25,11])
intersect(s1, s2)
producesSet([25])
setdiff(s1, s2)
producesSet{Any}([11])
, whereassetdiff(s2, s1)
producesSet([ 3.14])
issubset(s1, s2)
producesfalse
, butissubset(s1, Set([11, 25, 36]))
producestrue
To add an element to a set is easy: push!(s1, 32)
adds 32
to set s1
. Adding an existing...