我有一个数组
[1,5,3,3,4,4,4,5,6,6,6,6,8,9,1,1,5]
这里,“4”连续重复3次,“6”重复4次。数组长度不是固定的。
所以我需要迭代这个数组来找到连续重复的最大值。所以,4和6都是重复的,但最高的是6,所以函数应该在短时间内返回6及其起始索引和结束索引。
我还有一个病例2。
[10,11,10,10,11,10,15,16,20,21,22,21,21,20,20]
在上述情况下,值之间的微小差异可以忽略不计。例如。
第一个序列
10,11,10,10,11,10
,第二个序列20,21,22,21,21,20,20
,重复性最高的是第二个序列如果公差为1,则将考虑第一种情况而不是第二种情况。因为如果公差是1,那么第二序列将是
[21,21,20,20]
,因此出现的最大数目(计数)是第一种情况。任何帮助都将不胜感激。
最佳答案
我制作了一个reduce映射来查找最大发生结果,我向Int数组添加了一个枚举映射来获取reduce映射中索引的值,所有代码都用Swift 4进行了测试。
排序方法:
let array: [(Int, Int)] = [1, 5, 3, 3, 4, 4, 4, 5, 6, 6, 6, 6, 8, 9, 1, 1, 5].enumerated().map { ($0, $1) }
var results: [Result] = []
guard let maxResult = array.reduce(nil, { (maxResult, item) -> Result? in
let (index, value) = item
guard let result = results.first(where: { (result) -> Bool in
result.value == value && result.endIndex + 1 == index
}) else {
let result = Result(value, startIndex: index)
results.append(result)
return result > maxResult ? result : maxResult
}
result.endIndex = index
return result > maxResult ? result : maxResult
}) else {
print("no max result found :(")
return
}
print("found max result: \(maxResult) in results: \(results)")
结果类:
class Result {
let value: Int
var numberOfOcurrences: Int
let startIndex: Int
var endIndex: Int {
didSet {
numberOfOcurrences += 1
}
}
init(_ value: Int, startIndex: Int) {
self.value = value
self.startIndex = startIndex
self.endIndex = startIndex
self.numberOfOcurrences = 1
}
static func > (lhs: Result, rhs: Result?) -> Bool {
return lhs.numberOfOcurrences > rhs?.numberOfOcurrences ?? 0
}
}
extension Result: CustomStringConvertible {
var description: String {
return """
Result(
value: \(value),
numberOfOcurrences: \(numberOfOcurrences),
startIndex: \(startIndex),
endIndex: \(endIndex)
)
"""
}
}
输出:
/* output: found max result: Result(
value: 6,
numberOfOcurrences: 4,
startIndex: 8,
endIndex: 11
) in results: [
Result(
value: 1,
numberOfOcurrences: 1,
startIndex: 0,
endIndex: 0
),
Result(
value: 5,
numberOfOcurrences: 1,
startIndex: 1,
endIndex: 1
),
Result(
value: 3,
numberOfOcurrences: 2,
startIndex: 2,
endIndex: 3
),
Result(
value: 4,
numberOfOcurrences: 3,
startIndex: 4,
endIndex: 6
),
Result(
value: 5,
numberOfOcurrences: 1,
startIndex: 7,
endIndex: 7
),
Result(
value: 6,
numberOfOcurrences: 4,
startIndex: 8,
endIndex: 11
),
Result(
value: 8,
numberOfOcurrences: 1,
startIndex: 12,
endIndex: 12
),
Result(
value: 9,
numberOfOcurrences: 1,
startIndex: 13,
endIndex: 13
),
Result(
value: 1,
numberOfOcurrences: 2,
startIndex: 14,
endIndex: 15
),
Result(
value: 5,
numberOfOcurrences: 1,
startIndex: 16,
endIndex: 16
)
]
*/
更新:对于案例2
let array: [(Int, Int)] = [10, 11, 10, 10, 11, 10, 15, 16, 20, 21, 22, 21, 21, 20, 20].enumerated().map { ($0, $1) }
var results: [Result] = []
let tolerance: Int = 1 // now there can be one other in between
guard let maxResult = array.reduce(nil, { (maxResult, item) -> Result? in
let (index, value) = item
guard let result = results.first(where: { (result) -> Bool in
return result.value == value && result.endIndex + (1 + tolerance) >= index
}) else {
let result = Result(value, startIndex: index)
results.append(result)
return result > maxResult ? result : maxResult
}
result.endIndex = index
return result > maxResult ? result : maxResult
}) else {
print("no max result found :(")
return
}
print("found max result: \(maxResult) in results: \(results)")
输出:
/* output: found max result: Result(
value: 10,
numberOfOcurrences: 4,
startIndex: 0,
endIndex: 5
) in results: [
Result(
value: 10,
numberOfOcurrences: 4,
startIndex: 0,
endIndex: 5
), Result(
value: 11,
numberOfOcurrences: 1,
startIndex: 1,
endIndex: 1
), Result(
value: 11,
numberOfOcurrences: 1,
startIndex: 4,
endIndex: 4
), Result(
value: 15,
numberOfOcurrences: 1,
startIndex: 6,
endIndex: 6
), Result(
value: 16,
numberOfOcurrences: 1,
startIndex: 7,
endIndex: 7
), Result(
value: 20,
numberOfOcurrences: 1,
startIndex: 8,
endIndex: 8
), Result(
value: 21,
numberOfOcurrences: 3,
startIndex: 9,
endIndex: 12
), Result(
value: 22,
numberOfOcurrences: 1,
startIndex: 10,
endIndex: 10
), Result(
value: 20,
numberOfOcurrences: 2,
startIndex: 13,
endIndex: 14
)
]
*/
更新3:编辑结果类以按顺序存储值的数量
class Result {
let value: Int
var numberOfOcurrences: Int
var numberOfValuesInSequence: Int
let startIndex: Int
var endIndex: Int {
didSet {
numberOfOcurrences += 1
numberOfValuesInSequence = (endIndex - startIndex) + 1
}
}
init(_ value: Int, startIndex: Int) {
self.value = value
self.startIndex = startIndex
self.endIndex = startIndex
self.numberOfOcurrences = 1
self.numberOfValuesInSequence = 1
}
static func > (lhs: Result, rhs: Result?) -> Bool {
return lhs.numberOfValuesInSequence > rhs?.numberOfValuesInSequence ?? 0
}
}
extension Result: CustomStringConvertible {
var description: String {
return """
Result(
value: \(value),
numberOfOcurrences: \(numberOfOcurrences),
numberOfValuesInSequence: \(numberOfValuesInSequence),
startIndex: \(startIndex),
endIndex: \(endIndex)
)
"""
}
}
输出:
Result(
value: 10,
numberOfOcurrences: 4,
numberOfValuesInSequence: 6,
startIndex: 0,
endIndex: 5
)