How do I create and initialize arrays, sets, and dictionaries in Swift?

In Swift, you can create and initialize arrays, sets, and dictionaries easily. Below are examples for each of these data structures:

Arrays

// Creating and initializing an array var fruits: [String] = ["Apple", "Banana", "Cherry"] // Accessing an element let firstFruit = fruits[0] // "Apple"

Sets

// Creating and initializing a set var uniqueNumbers: Set = [1, 2, 3, 4, 5] // Adding an element uniqueNumbers.insert(6)

Dictionaries

// Creating and initializing a dictionary var ages: [String: Int] = ["Alice": 25, "Bob": 30] // Accessing a value let aliceAge = ages["Alice"] // 25

Swift arrays sets dictionaries data structures