<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Combine",
  "identifier" : "/documentation/Combine/Publisher/tryRemoveDuplicates(by:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Combine"
    ],
    "preciseIdentifier" : "s:7Combine9PublisherPAAE19tryRemoveDuplicates2byAA10PublishersO03TrydE0Vy_xGSb6OutputQz_ALtKc_tF"
  },
  "title" : "tryRemoveDuplicates(by:)"
}
-->

# tryRemoveDuplicates(by:)

Publishes only elements that don’t match the previous element, as evaluated by a provided error-throwing closure.

```
func tryRemoveDuplicates(by predicate: @escaping (Self.Output, Self.Output) throws -> Bool) -> Publishers.TryRemoveDuplicates<Self>
```

## Parameters

`predicate`

A closure to evaluate whether two elements are equivalent, for purposes of filtering. Return `true` from this closure to indicate that the second element is a duplicate of the first. If this closure throws an error, the publisher terminates with the thrown error.

## Return Value

A publisher that consumes — rather than publishes — duplicate elements.

## Discussion

Use [`tryRemoveDuplicates(by:)`](/documentation/Combine/Publisher/tryRemoveDuplicates(by:)) to remove repeating elements from an upstream publisher based upon the evaluation of elements using an error-throwing closure you provide. If your closure throws an error, the publisher terminates with the error.

In the example below, the closure provided to [`tryRemoveDuplicates(by:)`](/documentation/Combine/Publisher/tryRemoveDuplicates(by:)) returns `true` when two consecutive elements are equal, thereby filtering out `0`,
`1`, `2`, and `3`. However, the closure throws an error when it encounters `4`. The publisher then terminates with this error.

```
struct BadValuesError: Error {}
let numbers = [0, 0, 0, 0, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
cancellable = numbers.publisher
    .tryRemoveDuplicates { first, second -> Bool in
        if (first == 4 && second == 4) {
            throw BadValuesError()
        }
        return first == second
    }
    .sink(
        receiveCompletion: { print ("\($0)") },
        receiveValue: { print ("\($0)", terminator: " ") }
     )

 // Prints: "0 1 2 3 4 failure(BadValuesError()"
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)