Collections ยท SWIFT RECIPE

Filter an array

Select values that satisfy a condition.

main.swift
let scores = [4, 8, 2, 9, 6]
print(scores.filter { $0 >= 6 })

How it works

filter applies a Boolean predicate and returns matching elements in their input order. It does not mutate the source.

EXPECTED OUTPUT
[8, 9, 6]

Keep in mind

Use first(where:) when you only need one match.

Read the Swift language reference