How to Use Reduce in Swift

Use reduce in Swift when you need to combine a collection into one final result, such as totals, grouped strings, merged dictionaries, or derived summary values.

What you’ll build or solve

You’ll learn how to use reduce in Swift with numbers, strings, dictionaries, and model collections. You’ll also know when map or filter is clearer.

When this approach works best

This approach is the right choice when many values should collapse into one.

Common real-world scenarios include:

  • Total cart price
  • Score sums
  • CSV strings
  • Analytics counters
  • Merged settings

This is a bad idea when the output should stay a collection of similar size.

Prerequisites

You only need:

  • Basic Swift arrays
  • Familiarity with closures
  • Understanding of accumulator values

Step-by-step instructions

Step 1: Sum numeric values

The most common use is totals.

let prices = [19, 29, 49]

let total = prices.reduce(0) { partialResult, price in
    partialResult + price
}

This starts from 0 and adds every value.

The final result is one number.

Step 2: Build a string

reduce also works well for text.

let tags = ["swift", "ios", "ui"]

let csv = tags.reduce("") { result, tag in
    result.isEmpty ? tag : "\(result),\(tag)"
}

This builds a CSV string.

Step 3: Reduce model collections

A common app pattern is summing model values.

struct CartItem {
    let price: Double
}

let items = [
    CartItem(price: 9.99),
    CartItem(price: 14.99)
]

let total = items.reduce(0) { sum, item in
    sum + item.price
}

This is ideal for checkout screens.

Step 4: Merge dictionaries

reduce can build new collections too.

let pairs = [("theme", "dark"), ("lang", "en")]

let settings = pairs.reduce(into: [String: String]()) { result, pair in
    result[pair.0] = pair.1
}

This is cleaner than manual loops.

What to look for:

  • reduce collapses many values into one
  • Great for totals and summaries
  • Start with a sensible initial value
  • reduce(into:) is great for mutable builds
  • Use map for one-to-one transforms

Examples you can copy

Total score

scores.reduce(0, +)

CSV text

names.reduce("") { ... }

Cart total

items.reduce(0) { $0 + $1.price }

Common mistakes and how to fix them

Mistake 1: Using the wrong initial value

What the reader might do:

Start a numeric sum from 1.

Why it breaks: totals become incorrect.

Corrected approach:

Pick the neutral starting value.

Mistake 2: Using reduce when map is clearer

What the reader might do:

Use reduce for simple transforms.

Why it breaks: readability drops.

Corrected approach:

Use map.

Mistake 3: Building arrays inefficiently

What the reader might do:

Append repeatedly inside normal reduce.

Why it breaks: performance may suffer.

Corrected approach:

Use reduce(into:).

Troubleshooting

If totals look wrong, inspect the initial value.

If the closure feels hard to read, consider loops.

If arrays build slowly, switch to reduce(into:).

If the result should stay a collection, use map or filter.

Quick recap

  • Use reduce to combine many values into one
  • Great for totals and summaries
  • Choose the right initial value
  • Use reduce(into:) for collection builds
  • Prefer map for one-to-one transforms