Python Arrays, Sets & Higher-Order Collection Operations
Master Swift primary sequence collections. Learn ordered, type-safe Arrays ([Element]), unordered unique-element Sets (Set<Element>), set algebra (union, intersection), and functional pipelines with map, filter, reduce, and sorted.
"An Array is a sequential playlist where song order matters and songs can repeat. A Set is a collector bag of unique badges: adding a duplicate badge does nothing, and membership lookup is instant O(1)."
Deep Dive: How It Works
Array ([T]): Ordered collection with zero-indexed access (e.g. var fruits = ["Apple", "Banana"]).
Set (Set<T>): Unordered collection of unique values requiring elements to conform to the Hashable protocol.
Higher-Order Transformers: .map() transforms elements, .filter() keeps matching elements, .reduce() aggregates to single value.
Copy-on-Write: Collections are value types copied lazily on modification for maximum performance.
Syntax Blueprint
var list = [1, 2, 3]
list.append(4)
let evens = list.filter { $0 % 2 == 0 }
let unique = Set(list)Declare arrays with [], filter with closure shorthand $0, and deduplicate with Set.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Accessing an index >= array.count.Why it happens: Swift array subscripting bounds-checks and traps with a fatal error on out-of-bounds.
How to fix: Check index < array.count or use array.indices.contains(index).
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Deduplicate and Sort Numbers
Declare let rawNumbers = [5, 2, 8, 2, 5, 1, 9].
Convert to a Set to eliminate duplicates.
Sort the set into ascending order using .sorted().
Print "Sorted Unique: " followed by the sorted array.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.