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 easy as this:
// code in Chapter 5\sets.jl: s = Set({11, 14, 13, 7, 14, 11})
The Set()
function creates an empty set. The preceding line returns Set{Int64}({7,14,13,11})
, where the duplicates have been eliminated. From v0.4 onwards, the {}
notation with sets is deprecated; you should use s = Set(Any[11, 14, 13, 7, 14, 11])
. In the accompanying code file, the latest version is used.
The 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{Any}({3.14,25,11})
intersect(s1, s2)
producesSet{Any}({25})
setdiff(s1, s2)
producesSet{Any}({11})
, whereassetdiff(s2, s1)
producesSet...