0Pricing
Swift Academy · Lesson

reduce and reduce(into:)

Aggregate collections into a single value.

reduce and reduce(into:) is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)  // 10

Operator 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, *))   // 24

Reducing 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)  // 9

The 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 abc

Grouping 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: into

Quick 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 an inout accumulator — use it for collections
  • Great for counts, grouping, and joins

Next: flatMap and chaining.

Frequently asked questions

Is the “reduce and reduce(into:)” lesson free?

Yes — the full text of “reduce and reduce(into:)” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. map and compactMap
  2. filter and Predicates
  3. reduce and reduce(into:)
  4. flatMap and Chaining
← Back to Swift Academy