I am new to swift and i was wondering if i could get unique values based on the key, value pair from 2 arrays.
Array-1 :
let arr1 = [
["title" : "News", "icon" : "news_1"],
["title" : "Food", "icon" : "food_1"]
]
Array-2 :
let arr2 = [
["title" : "News", "icon" : "news_2"],
["title" : "Technology", "icon" : "tech_1"]
]
How to get a result something like
Array-3 :
let arr3 = [
["title" : "Food", "icon" : "food_1"],
["title" : "Technology", "icon" : "tech_1"]
]
So I need the set of all unique values from array-1 and array-2 based on key named "title" in a resulting array.
Note : Here i have title "food" in array-1 and 2 but the icon is different.
CodePudding user response:
What you are looking for is probably the symmetric difference for those two arrays. But as pointed out in the comments, your data structure is not well suited for this. If you really want to stick to this current structure, this might work for you:
func symmetricDifference(arrays: [[[String: String]]], by key: String) -> [[String: String]] {
var result = [String: [String: String]?]()
for array in arrays {
for element in array {
guard let keyValue = element[key] else {
// Ignore elements that do not contain the key, adjust as needed
continue
}
// Check if we already saw the value
if result[keyValue] == nil {
// Add the element
result[keyValue] = element
} else {
// We saw this keyValue already, remove the element
result.removeValue(forKey: keyValue)
}
}
}
// Only return non nil elements
return result.values.compactMap { $0 }
}
// Usage
symmetricDifference(arrays: [arr1, arr2], by: "title")
By using a better suited data structure, you could easily use the built-in Set operator:
struct Category: Hashable {
let title: String
let icon: String
func hash(into hasher: inout Hasher) {
hasher.combine(title)
}
static func ==(lhs: Category, rhs: Category) -> Bool {
lhs.title == rhs.title
}
}
let set1 = Set([Category(title: "News", icon: "news_1"), Category(title: "Food", icon: "food_1")])
let set2 = Set([Category(title: "News", icon: "news_2"), Category(title: "Technology", icon: "tech_1"), Category(title: "News", icon: "news_2")])
let result = set1.symmetricDifference(set2)