© Elshad Karimov 2020
E. KarimovData Structures and Algorithms in Swifthttps://doi.org/10.1007/978-1-4842-5769-2_3

3. Sets

Elshad Karimov1 
(1)
New York, New York, USA
 

It is an unordered collection (meaning you won't get the elements in the same order as you defined) of unique, non-nil elements. It must conform to the Hashable protocol . This means it has to provide a hashValue property. This is important because sets are unordered and hashValue is used to access the elements of the sets.

Access time is more efficient than arrays. When searching for an element inside an array, the worst scenario is O(n), where n is the size of the array, but in a set, it is always constant O(1). Unlike other collection types while declaring sets, a set type must be ...

Get Data Structures and Algorithms in Swift: Implement Stacks, Queues, Dictionaries, and Lists in Your Apps now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.