【发布时间】:2018-01-14 04:28:58
【问题描述】:
我们以我有这个数组为例:
let dates = ["01/02/16", "02/02/16", "03/02/16", "01/02/16", "02/02/16"]
我喜欢将每个String日期作为键存储在字典中,对于每个重复的键,我喜欢将它的索引存储为一个数组,并将其与重复键相关联。
例如,01/02/16 出现在索引 0 和 3。 02/02/16 出现在索引 1 和 4。
我喜欢这样的字典:
[ "01/02/16": [0, 3], "02/02/16": [2, 4], "03/02/16": [1] ]
我知道如何跟踪有多少重复条目,如下所示:
let dates = ["01/02/16", "01/02/16", "02/02/16", "03/02/16"]
var dateCounts:[String:Int] = [:]
for date in dates
{
dateCounts[date] = (dateCounts[date] ?? 0) + 1
}
for (key, value) in dateCounts
{
print("\(key) occurs \(value) time/s")
}
但是,我不确定如何跟踪重复索引?
【问题讨论】: