0

Let's say I have an array that looks like this:

let games = [
  Game(type: "Soccer", value: 1),
  Game(type: "Basket", value: 3),
  Game(type: "Hockey", value: 5),
  Game(type: "Soccer", value: 2),
  Game(type: "Soccer", value: 4),
  Game(type: "Basket", value: 2)
]

I want a new array with 1 row per type with the values added to each other with matching type. Something like this:

let newGames = [
  NewGame(type: "Soccer", value: 7),
  NewGame(type: "Basket", value: 5),
  NewGame(type: "Hockey", value: 5)
]

Help please. My brain is not cooperating.

0

3 Answers 3

3
struct Game {
    let type: String
    let value: Int
}

let games: [Game] = [
    Game(type: "Soccer", value: 1),
    Game(type: "Basket", value: 3),
    Game(type: "Hockey", value: 5),
    Game(type: "Soccer", value: 2),
    Game(type: "Soccer", value: 4),
    Game(type: "Basket", value: 2)
]

var totalValueForGame = [String: Int]()
for game in games {
    totalValueForGame[game.type] = game.value + (totalValueForGame[game.type] ?? 0)
}

let newGames = totalValueForGame.map { Game(type: $0, value: $1) }
print(newGames)

Output:

[Game(type: "Soccer", value: 7), Game(type: "Hockey", value: 5), Game(type: "Basket", value: 5)]
Sign up to request clarification or add additional context in comments.

Comments

1
class Game {
    var type: String
    var value: Int
    init(type: String, value: Int) {
        self.type = type
        self.value = value
    }
}

let games: [Game] = [
    Game(type: "Soccer", value: 1),
    Game(type: "Basket", value: 3),
    Game(type: "Hockey", value: 5),
    Game(type: "Soccer", value: 2),
    Game(type: "Soccer", value: 4),
    Game(type: "Basket", value: 2)
]
var gameTotals:[Game] = []
for key in Set(games.map{$0.type}) {
    gameTotals.append(Game(type: key, value: games.filter{$0.type == key}.reduce(0){$0+$1.value}))
}

gameTotals  // [{type "Soccer", value 7}, {type "Hockey", value 5}, {type "Basket", value 5}]

Comments

0

My (ugly) solution :D :D

import Cocoa

class Game {
    var name:String
    var value:Int

    init(withName aName:String, andValue value:Int) {
        self.name = aName
        self.value = value
    }
}

var someSports:[Game] = []
var tempDictOfGames:[String:Int] = [:]


var sports:[String] = ["Soccer", "Volleyball", "Tennis", "Rugby", "Tennis", "Soccer", "Tennis", "Cricket"]
var values:[Int] = [1, 5, 3, 8, 2, 9, 4, 7]


for (var i = 0; i < sports.count; i++) {

    var availableGameValue:Int? = tempDictOfGames[sports[i]]

    if let myValue = availableGameValue {
        tempDictOfGames[sports[i]]! += values[i]
    } else {
        tempDictOfGames[sports[i]] = values[i]
    }
}

for (k, v) in tempDictOfGames {
    let newGame = Game(withName: k, andValue: v)
    someSports.append(newGame)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.