Sobes.tech
Junior — Middle

Can you name the main types of collections used in Swift?

sobes.tech AI

Answer from AI

In the Swift language, the main collection types include:

  • Array — an ordered list of elements of the same type. Allows storing elements with index access.

  • Set — an unordered collection of unique elements. Fast search and presence check.

  • Dictionary — a collection of key-value pairs, where keys are unique. Allows quick retrieval of values by key.

Example usage:

var numbers: [Int] = [1, 2, 3]
var uniqueNames: Set<String> = ["Anna", "Bob", "Anna"] // contains "Anna" and "Bob"
var ages: [String: Int] = ["Alice": 30, "Bob": 25]