---
title: "filter(_:)"
framework: swift
role: symbol
role_heading: Instance Method
path: "swift/set/filter(_:)"
---

# filter(_:)

Returns a new set containing the elements of the set that satisfy the given predicate.

## Declaration

```swift
consuming func filter<E>(_ isIncluded: (Element) throws(E) -> Bool) throws(E) -> Set<Element> where E : Error
```

## Parameters

- `isIncluded`: A closure that takes an element as its argument and returns a Boolean value indicating whether the element should be included in the returned set.

## Return Value

Return Value A set of the elements that isIncluded allows.

## Discussion

Discussion In this example, filter(_:) is used to include only names shorter than five characters. let cast: Set = ["Vivien", "Marlon", "Kim", "Karl"] let shortNames = cast.filter { $0.count < 5 }

shortNames.isSubset(of: cast) // true shortNames.contains("Vivien") // false

## See Also

### Removing Elements

- [remove(_:)](swift/set/remove(_:)-8p2tv.md)
- [remove(_:)](swift/set/remove(_:)-4d3i1.md)
- [removeFirst()](swift/set/removefirst().md)
- [remove(at:)](swift/set/remove(at:).md)
- [removeAll(keepingCapacity:)](swift/set/removeall(keepingcapacity:).md)
