Kevvv
Kevvv

Reputation: 4023

Finding the sum of the values of a dictionary with matching keys

I have a nested dictionary of type [String: [String: Int]] that looks like this:

var dict = [
    "A": ["a": 234, "b": 322],
    "B": ["c": 45, "d": 443],
    "C": ["b": 50, "e": 940, "f": 9430, "a": 53029]
]

I want to convert it so that the integers of all the same lowercased keys are added together to form another dictionary:

["a": 53263, "b": 382, "c": 45, "d": 50, "e": 940, "f": 9430]

I don't care for the keys "A", "B", "C".

First, I mapped dict to extract the nested dictionary:

let result1 = dict.map { $1 }
// [["b": 322, "a": 234], ["c": 45, "d": 443], ["e": 940, "b": 50, "a": 53029, "f": 9430]]

I used the flatMap to get the tuple:

let result2 = dict.map { $1 }.flatMap { $0.map { $0 }}
// [(key: "c", value: 45), (key: "d", value: 443), (key: "f", value: 9430), (key: "e", value: 940), (key: "a", value: 53029), (key: "b", value: 50), (key: "a", value: 234), (key: "b", value: 322)]

Finally, I tried to take result2 and map over each item to add all the values of the same keys and return a dictionary of type [String: Int]:

var tempArray = [String]()
var finalDict = [String: Int]()
var count = 0
let result3 = dict.map { $1 }.flatMap { $0.map { el -> [String: Int] in
    if tempArray.contains(el.key) {
        if let newCount = finalDict[el.key] {
            count += newCount
            finalDict[el.key] = newCount
        }
    } else {
        tempArray.append(el.key)
        finalDict = [el.key: el.value]
    }

    return finalDict
}}

But, the result I'm getting is:

[["b": 322], ["a": 234], ["c": 45], ["d": 443], ["d": 443], ["d": 443], ["f": 9430], ["e": 940]]

which shows multiples of the same keys.

Upvotes: 2

Views: 470

Answers (4)

Leo Dabus
Leo Dabus

Reputation: 236458

You can use dictionary uniquingKeysWith initializer and use the plus method to sum them up:


let result: Dictionary = .init(dict.flatMap(\.value), uniquingKeysWith: +)

Upvotes: 0

New Dev
New Dev

Reputation: 49590

You're almost there. result2 is an array of tuples, which you could use to construct a dictionary with unique keys by adding their values:

var dict = Dictionary(result2, uniquingKeysWith: +)

which will give you what you need (order not guaranteed):

["c": 45, "a": 53263, "d": 443, "b": 372, "f": 9430, "e": 940]

There's a slightly simpler way to get your result2:

let result2 = dict.flatMap { $0.value }.flatMap { $0.map{ $0 } }

Upvotes: 2

Joakim Danielson
Joakim Danielson

Reputation: 52013

You can use flatMap to create one array of tuples and then reduce(into:) to create a dictionary with the keys and the sum of the values per key

let result = dict.values
    .flatMap {$0}
    .reduce(into: [:]) { $0[$1.key, default: 0] += $1.value }

Upvotes: 2

Jithin
Jithin

Reputation: 943

You can convert dict to an array of dictionaries and then use reduce to merge the keys.

let values = dict.map {$0.value}.flatMap {$0}

let merged = values .flatMap{$0}.reduce([String:Int]()) { (previous, dict) in
    var newDict = previous
    if let lastValue = previous[dict.key] {
        let sum = lastValue + dict.value
        newDict[dict.key] = sum
    } else {
        newDict.updateValue(dict.value, forKey: dict.key)
    }
    return newDict
}

print(merged)

Upvotes: 0

Related Questions