<!--
{
  "availability" : [
    "iOS: 27.0.0 -",
    "iPadOS: 27.0.0 -",
    "macCatalyst: 27.0.0 -",
    "macOS: 27.0.0 -",
    "tvOS: 27.0.0 -",
    "visionOS: 27.0.0 -",
    "watchOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/UniqueArray/replaceSubrange(_:copying:)-70i0j",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s11UniqueArrayV15replaceSubrange_7copyingySnySiG_s4SpanVyxGtF"
  },
  "title" : "replaceSubrange(_:copying:)"
}
-->

# replaceSubrange(_:copying:)

Replaces the specified subrange of elements by copying the elements of
the given span.

```
mutating func replaceSubrange(_ subrange: Range<Int>, copying newElements: Span<Element>)
```

## Parameters

`subrange`

The subrange of the array to replace. The bounds of
the range must be valid indices in the array.

`newElements`

The new elements to copy into the collection.

## Discussion

This method has the effect of removing the specified range of elements
from the array and inserting the new elements starting at the same location.
The number of new elements need not match the number of elements being
removed.

If the capacity of the array isn’t sufficient to perform the replacement,
then this reallocates the array’s storage to extend its capacity, using a
geometric growth rate.

If you pass a zero-length range as the `subrange` parameter, this method
inserts the elements of `newElements` at `subrange.lowerBound`. Calling
the `insert(copying:at:)` method instead is preferred in this case.

Likewise, if you pass a zero-length span as the `newElements`
parameter, this method removes the elements in the given subrange
without replacement. Calling the `removeSubrange(_:)` method instead is
preferred in this case.

> Complexity: O(*n* + *m*), where *n* is count of this array and
> *m* is the count of `newElements`.

---

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)