ios - Finding and combining duplicate entries in an array -


im new here correct me if i've formatted incorrectly (also new swift) i'm trying take array of dates , numbers , if of dates same combine numbers 1 entry.

so this

//this how format data after pull core data var dateandentry = [(string, double)]  //i've split them 2 seperate arrays sorting, feel theres better way 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

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 can makes new array contains new values rather allowing me duplicated values, combine, insert @ index delete original entries in 2 arrays.

func combineduplicates(dates: [string]) -> [string]{     var output: [string] = []     var checked = set<string>()     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 have looked on google , here answers turned nothing.

any solutions, explanations, help, pointers or references other questions or sources may have missed appreciated.

this dictionary keys dates , values sum of entries 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] 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -