2

Im new here so correct me if I've formatted this incorrectly (also new to swift) but basically what I'm trying to do is take an array of dates and numbers and if any of the dates are the same combine the numbers into one entry.

so this

//This is how i format the data after i pull it from core data
var dateAndEntry = [(String, Double)]

//i've split them into two seperate arrays for sorting, but I feel like theres a better way i don't know
var dates = ["01/01/2016", "02/01/2016", "02/01/2016", "04/01/2016", "05/01/2016", "06/01/2016","07/01/2016","07/01/2016"]
var entries = [-14,2,8,9,-1,8,25,6]

becomes this

var dates = ["01/01/2016", "02/01/2016", "04/01/2016", "05/01/2016", "06/01/2016","07/01/2016"]
var entries = [-14,10,9,-1,8,19]

I've tried doing this but i can only get it so that it makes a new array that only contains the new values rather than allowing me to get the duplicated values, combine, insert at index then delete original entries in the two arrays.

func combineDuplicates(dates: [String]) -> [String]{
    var output: [String] = []
    var checked = Set<String>()
    for date in dates {

        if !checked.contains(date) {
            checked.insert(date)
            output.append(date)
        }
    }
    print(output)
    return output
}

let sorted = combineDuplicates(dates)
print(sorted)

and yes i have looked on google and here for answers but turned up nothing.

Any solutions, explanations, help, pointers or references to other questions or sources I may have missed would all be greatly appreciated.

Cristik
  • 30,989
  • 25
  • 91
  • 127
Omnom
  • 23
  • 3

1 Answers1

3

This will get you a dictionary with keys the dates and values the sum of entries for each date:

dates.enumerate().reduce([String:Int]()) { dict, item in
    var dict = dict
    dict[item.1] = (dict[item.1] ?? 0) + entries[item.0]
    return dict
}
// ["06/01/2016": 8, "04/01/2016": 9, "01/01/2016": -14, "05/01/2016": -1, "07/01/2016": 31, "02/01/2016": 10]
Cristik
  • 30,989
  • 25
  • 91
  • 127
  • 1
    damn, too slow. Add `.map { $0 }` at the end to turn it back into an array. – R Menke Jan 05 '16 at 23:15
  • @RMenke you can add an answer referring to mine, plus the `map` call. If would be a shame for people to not see your suggestion which completes my answer. – Cristik Jan 05 '16 at 23:21
  • nah, my answer was 11 lines. Yours is better! – R Menke Jan 05 '16 at 23:23
  • You can write only the comment you posted, I forgot about the `map()` which will give the tuples in the exact format the OP needs, and I would not feel good amending my answer with your suggestion. – Cristik Jan 05 '16 at 23:29
  • 2
    Don't worry about it. If I cared about the credit or the rep I would have posted it as answer. I only care about forming the best possible answer. In this case it is yours. – R Menke Jan 05 '16 at 23:35
  • No need to make a copy of the dictionary just make it variable adding var there. Check this answer http://stackoverflow.com/a/31447400/2303865 – Leo Dabus Jan 06 '16 at 00:10
  • @LeoDabus won't that be removed from Swift? – R Menke Jan 06 '16 at 00:59
  • 1
    @RMenke the syntax it is old. I am just showing Cristik that he can drop `var dict = dict` declaring `dates.enumerate().reduce([String:Int]()) { var dict, item in` – Leo Dabus Jan 06 '16 at 01:01
  • @LeoDabus I know but I was wondering if `var` in that context is a function parameter. In which case it will no longer work in Swift3 – R Menke Jan 06 '16 at 01:04
  • @RMenke I don't know. I haven't used Swift 3.0 – Leo Dabus Jan 06 '16 at 01:06
  • @RMenke where can I download Xcode beta? It disappeared from Xcode's download page. The only Xcode available for download now it is the AppStore version – Leo Dabus Jan 06 '16 at 01:12
  • 1
    @LeoDabus [proposed/accepted changes Swift3](https://github.com/apple/swift-evolution/blob/master/proposals/0003-remove-var-parameters-patterns.md) – R Menke Jan 06 '16 at 01:12
  • @LeoDabus I know, and I have no idea. They pulled it after Swift went open source. – R Menke Jan 06 '16 at 01:14
  • Would it be more efficient for me to store them as NSDate objects and sort them instead of having them as strings? – Omnom Jan 10 '16 at 18:10
  • If you need them in the end to be `NSDate`, definitively it would be more efficient to convert the string to `NSDate` a-priori processing them. – Cristik Jan 10 '16 at 18:16