← Back to Swift Course | Chapter 6: Collections | Lesson 4 of 7

Common Collection Operations

Collection operations are handy built-in tricks, like counting, sorting, or checking if something's inside, that work the same way across arrays, sets, and dictionaries.
Syntax
swift
collection.count
collection.isEmpty

let copy = array.sorted()   // new sorted array
array.sort()                // sorts in place
array.first                 // Type? (optional)
array.last
array.reversed()

count and isEmpty

These two properties work the same way across arrays, sets, and dictionaries, letting you check a collection's size without writing a loop.

Example: count and isEmpty

markup
// Declare a constant `numbers`, set to `[5, 3, 8, 1]`
let numbers = [5, 3, 8, 1]
// Print "Count: \(numbers.count)" to the console
print("Count: \(numbers.count)")
// Print "Is empty: \(numbers.isEmpty)" to the console
print("Is empty: \(numbers.isEmpty)")

sorted() vs sort()

.sorted() returns a brand-new sorted array leaving the original untouched, while .sort() sorts the array in place and requires it to be a var.

Example: sorted() vs sort()

markup
// Declare a constant `original`, set to `[5, 2, 8, 1]`
let original = [5, 2, 8, 1]
// Declare a constant `sortedCopy`, set to `original.sorted()`
let sortedCopy = original.sorted()
// Print "Original: \(original)" to the console
print("Original: \(original)")
// Print "Sorted copy: \(sortedCopy)" to the console
print("Sorted copy: \(sortedCopy)")

first, last, and reversed

.first and .last safely return optionals rather than crashing on empty collections, and .reversed() provides the elements in reverse order.

Example: first, last, and reversed

markup
// Declare a constant `letters`, set to `["a", "b", "c"]`
let letters = ["a", "b", "c"]
// Print `letters.first as Any` to the console
print(letters.first as Any)
// Print `letters.last as Any` to the console
print(letters.last as Any)
// Print `Array(letters.reversed())` to the console
print(Array(letters.reversed()))
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing a manual loop to check if a collection is empty instead of using the built-in .isEmpty property.
  2. Forgetting .count works on all collection types uniformly (arrays, sets, dictionaries, strings).
  3. Sorting an array with .sort() when you meant .sorted() -- one mutates in place, the other returns a new array.
Chapter Summary
  • .count and .isEmpty work consistently across arrays, sets, and dictionaries.
  • .sorted() returns a new sorted array without modifying the original; .sort() sorts in place.
  • .first and .last safely return optionals instead of crashing on an empty collection.
  • .reversed() returns elements in reverse order as a lazy sequence.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.