---
title: "init(_:uniquingKeysWith:)"
framework: swift
role: symbol
role_heading: Initializer
path: "swift/dictionary/init(_:uniquingkeyswith:)"
---

# init(_:uniquingKeysWith:)

Creates a new dictionary from the key-value pairs in the given sequence, using a combining closure to determine the value for any duplicate keys.

## Declaration

```swift
init<S, E>(_ keysAndValues: S, uniquingKeysWith combine: (Value, Value) throws(E) -> Value) throws(E) where S : Sequence, E : Error, S.Element == (Key, Value)
```

## Parameters

- `keysAndValues`: A sequence of key-value pairs to use for the new dictionary.
- `combine`: A closure that is called with the values for any duplicate keys that are encountered. The closure returns the desired value for the final dictionary.

## Discussion

Discussion You use this initializer to create a dictionary when you have a sequence of key-value tuples that might have duplicate keys. As the dictionary is built, the initializer calls the combine closure with the current and new values for any duplicate keys. Pass a closure as combine that returns the value to use in the resulting dictionary: The closure can choose between the two values, combine them to produce a new value, or even throw an error. The following example shows how to choose the first and last values for any duplicate keys: let pairsWithDuplicateKeys = [("a", 1), ("b", 2), ("a", 3), ("b", 4)]

let firstValues = Dictionary(pairsWithDuplicateKeys,                              uniquingKeysWith: { (first, _) in first }) // ["b": 2, "a": 1]

let lastValues = Dictionary(pairsWithDuplicateKeys,                             uniquingKeysWith: { (_, last) in last }) // ["b": 4, "a": 3]

## See Also

### Creating a Dictionary

- [init()](swift/dictionary/init().md)
- [init(minimumCapacity:)](swift/dictionary/init(minimumcapacity:).md)
- [init(uniqueKeysWithValues:)](swift/dictionary/init(uniquekeyswithvalues:).md)
- [init(grouping:by:)](swift/dictionary/init(grouping:by:).md)
