Sets

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) produces Set([3.14,25,11])
  • intersect(s1, s2) produces Set([25]) ...

Get Julia 1.0 Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.