Complete the code to extend Array only when its elements conform to Equatable.
extension Array where Element: [1] { func containsElement(_ element: Element) -> Bool { return self.contains(element) } }
The extension applies only when the array's elements conform to Equatable, allowing use of the contains method.
Complete the code to add a method only for Dictionary where Key is String.
extension Dictionary where [1] == String { func allKeys() -> [String] { return Array(self.keys) } }
The extension applies only when the Key type of the dictionary is String.
Fix the error in the extension to add a method only when Element conforms to Numeric.
extension Array where Element: [1] { func sum() -> Element { return self.reduce(0, +) } }
The sum method requires elements to conform to Numeric so that addition and zero are defined.
Fill both blanks to extend Sequence only when Element is Hashable and add a method to get unique elements.
extension Sequence where [1]: [2] { func uniqueElements() -> [Element] { var seen = Set<Element>() return self.filter { seen.insert($0).inserted } } }
The extension requires the Element to conform to Hashable so it can be stored in a Set to filter unique values.
Fill all three blanks to extend Optional only when Wrapped is Comparable and add a method to compare with another Optional.
extension Optional where [1]: [2] { func isGreater(than other: Optional<[3]>) -> Bool { switch (self, other) { case let (.some(a), .some(b)): return a > b default: return false } } }
The extension applies when Wrapped type conforms to Comparable. The method compares two optionals by unwrapping and comparing their values.