reduce and reduce(into:)
Aggregate collections into a single value.
reduce and reduce(into:) is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
What Is reduce?
reduce collapses a whole sequence into a single value by repeatedly combining an accumulator with each element.
- You give a starting value
- And a closure
(accumulator, element) -> accumulator
Summing with reduce
The classic example — add everything up:
let nums = [1, 2, 3, 4]
let total = nums.reduce(0) { acc, x in acc + x }
print(total) // 10Operator Shorthand
When the closure is just an operator, you can pass the operator function directly:
let nums = [1, 2, 3, 4]
print(nums.reduce(0, +)) // 10
print(nums.reduce(1, *)) // 24Reducing to a Different Type
The result type can differ from the element type:
let words = ["a", "b", "c"]
let joined = words.reduce("") { $0 + $1.uppercased() }
print(joined) // "ABC"Finding a Max with reduce
You can compute aggregates beyond sums:
let nums = [3, 9, 2, 7]
let maxVal = nums.reduce(Int.min) { max($0, $1) }
print(maxVal) // 9The Cost of reduce
Plain reduce creates a brand new accumulator each step. For value types like String or Array that means copying the whole accumulator on every element — O(n²) work for large inputs.
Enter reduce(into:)
reduce(into:) passes the accumulator as an inout parameter, so you mutate it in place instead of copying:
let nums = [1, 2, 2, 3, 3, 3]
let counts = nums.reduce(into: [Int: Int]()) { dict, n in
dict[n, default: 0] += 1
}
print(counts) // [1: 1, 2: 2, 3: 3]Building an Array Efficiently
reduce(into:) shines when accumulating into a collection:
let nums = [1, 2, 3, 4, 5]
let evensSquared = nums.reduce(into: [Int]()) { result, n in
if n % 2 == 0 { result.append(n * n) }
}
print(evensSquared) // [4, 16]reduce vs reduce(into:)
Same result, different performance characteristics:
let parts = ["a", "b", "c"]
let a = parts.reduce("") { $0 + $1 } // copies each step
let b = parts.reduce(into: "") { $0 += $1 } // mutates in place
print(a, b) // abc abcGrouping with reduce(into:)
A frequent pattern: bucket items by a key:
let names = ["Ann", "Al", "Bo", "Cy"]
let byInitial = names.reduce(into: [Character: [String]]()) { dict, name in
dict[name.first!, default: []].append(name)
}
print(byInitial[Character("A")]!) // ["Ann", "Al"]Choosing Between Them
Use reduce for cheap scalar accumulators (Int, Double). Use reduce(into:) whenever the accumulator is a collection or other large value type to avoid copies.
let nums = [1, 2, 3]
print(nums.reduce(0, +)) // scalar: plain reduce
print(nums.reduce(into: Set<Int>()) { $0.insert($1) }) // collection: intoQuick Check
Why prefer reduce(into:) when building a dictionary or array?
Recap
You learned reduction:
reduce(initial) { acc, x in ... }collapses a sequence to one value- Operator shorthand:
reduce(0, +) reduce(into:)mutates aninoutaccumulator — use it for collections- Great for counts, grouping, and joins
Next: flatMap and chaining.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “reduce and reduce(into:)” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “reduce and reduce(into:)”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
What will I learn in “reduce and reduce(into:)”?
Aggregate collections into a single value. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “reduce and reduce(into:)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- map and compactMap
- filter and Predicates
- reduce and reduce(into:)
- flatMap and Chaining