Difference between Array, Set and Dictionary in Swift

Here are the practical differences between the different types:

Arrays are effectively ordered lists and are used to store lists of information in cases where order is important.

For example, posts in a social network app being displayed in a tableView may be stored in an array.

Sets are different in the sense that order does not matter and these will be used in cases where order does not matter.

Sets are especially useful when you need to ensure that an item only appears once in the set.

Dictionaries are used to store key, value pairs and are used when you want to easily find a value using a key, just like in a dictionary.

For example, you could store a list of items and links to more information about these items in a dictionary.

Hope this helps :)

(For more information and to find Apple's own definitions, check out Apple's guides at https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html)


Old thread yet worth to talk about performance.

With given N element inside an array or a dictionary it worth to consider the performance when you try to access elements or to add or to remove objects.

Arrays

To access a random element will cost you the same as accessing the first or last, as elements follow sequentially each other so they are accessed directly. They will cost you 1 cycle.

Inserting an element is costly. If you add to the beginning it will cost you 1 cycle. Inserting to the middle, the remainder needs to be shifted. It can cost you as much as N cycle in worst case (average N/2 cycles). If you append to the end and you have enough room in the array it will cost you 1 cycle. Otherwise the whole array will be copied which will cost you N cycle. This is why it is important to assign enough space to the array at the beginning of the operation.

Deleting from the beginning or the end it will cost you 1. From the middle shift operation is required. In average it is N/2.

Finding element with a given property will cost you N/2 cycle.

So be very cautious with huge arrays.

Dictionaries

While Dictionaries are disordered they can bring you some benefits here. As keys are hashed and stored in a hash table any given operation will cost you 1 cycle. Only exception can be finding an element with a given property. It can cost you N/2 cycle in the worst case. With clever design however you can assign property values as dictionary keys so the lookup will cost you 1 cycle only no matter how many elements are inside.


Detailed documentation can be found here on Apple's guide. Below are some quick definations extracted from there:

Array

An array stores values of the same type in an ordered list. The same value can appear in an array multiple times at different positions.

Set

A set stores distinct values of the same type in a collection with no defined ordering. You can use a set instead of an array when the order of items is not important, or when you need to ensure that an item only appears once.

Dictionary

A dictionary stores associations between keys of the same type and values of the same type in a collection with no defined ordering. Each value is associated with a unique key, which acts as an identifier for that value within the dictionary. Unlike items in an array, items in a dictionary do not have a specified order. You use a dictionary when you need to look up values based on their identifier, in much the same way that a real-world dictionary is used to look up the definition for a particular word.