Arrays 从Swift中的数组中删除较小的值集
给定一个由包含整数的数组组成的数组Arrays 从Swift中的数组中删除较小的值集,arrays,swift,Arrays,Swift,给定一个由包含整数的数组组成的数组 [[2], [3], [2, 2], [5], [7], [2, 2, 2], [3, 3]] 在Swift中,删除包含少量具有特定值的元素的数组并仅保留包含该值的较大数组的首选方式是什么 上述输入的结果将是 [[5], [7], [2, 2, 2], [3, 3]] 使用[Int:[Int]]字典跟踪键指定值的最大数组 let arrays = [[2], [3], [2, 2], [5], [7], [2, 2, 2], [3, 3]] var lar
[[2], [3], [2, 2], [5], [7], [2, 2, 2], [3, 3]]
在Swift中,删除包含少量具有特定值的元素的数组并仅保留包含该值的较大数组的首选方式是什么
上述输入的结果将是
[[5], [7], [2, 2, 2], [3, 3]]
使用
[Int:[Int]]
字典跟踪键指定值的最大数组
let arrays = [[2], [3], [2, 2], [5], [7], [2, 2, 2], [3, 3]]
var largest = [Int: [Int]]()
for arr in arrays {
// Get the first value from the array
if let first = arr.first {
// current is the count for that key already in dictionary largest
// If the key isn't found, the nil coalescing operator ?? will
// return the default count of 0.
let current = largest[first]?.count ?? 0
// If our new array has a larger count, put it in the dictionary
if arr.count > current {
largest[first] = arr
}
}
}
// Convert the dictionary's values to an array for the final answer.
let result = Array(largest.values)
print(result) // [[5], [7], [2, 2, 2], [3, 3]]
此相同逻辑可与
reduce
一起使用,以在一行中提供结果:
let result = arrays.reduce([Int: [Int]]()) { var d = $0; guard let f = $1.first else { return d }; d[f] = d[f]?.count > $1.count ? d[f] : $1; return d }.map { $1 }
let result = arrays.reduce([Int: Int]()) { var d = $0; guard let f = $1.first else { return d }; d[f] = max(d[f] ?? 0, $1.count); return d }.map { [Int](count: $1, repeatedValue: $0) }
备用版本 此版本使用
[Int:Int]
字典只保留每个键找到的最大数组的计数,然后在最后使用数组构造函数重建数组
let arrays = [[2], [3], [2, 2], [5], [7], [2, 2, 2], [3, 3]]
var counts = [Int: Int]()
for arr in arrays {
if let first = arr.first {
counts[first] = max(counts[first] ?? 0, arr.count)
}
}
let result = counts.map { [Int](count: $1, repeatedValue: $0) }
print(result) // [[5], [7], [2, 2, 2], [3, 3]]
此相同逻辑可与
reduce
一起使用,以在一行中提供结果:
let result = arrays.reduce([Int: [Int]]()) { var d = $0; guard let f = $1.first else { return d }; d[f] = d[f]?.count > $1.count ? d[f] : $1; return d }.map { $1 }
let result = arrays.reduce([Int: Int]()) { var d = $0; guard let f = $1.first else { return d }; d[f] = max(d[f] ?? 0, $1.count); return d }.map { [Int](count: $1, repeatedValue: $0) }
我正要写下我的答案,这时我看到瓦卡瓦马的回答非常相似。不过,我还是决定回到这个话题上来,因为这是一个有趣的问题。因此,我的替代方案几乎肯定比vacawama的解决方案慢得多,并且不能保持顺序,但我认为这是一个有趣的例子,作为解决Swift中类似问题的替代方案
var items = [[2], [3], [2, 2], [5], [7], [2, 2, 2], [3, 3]]
let reduced = items.sort({
let lhs = $0.first, rhs = $1.first
return lhs == rhs ? $0.count > $1.count : lhs < rhs
}).reduce( [[Int]]()) { (res, items) in
return res.last?.last != items.last ? res + [items] : res
}
print(reduced) // [[2, 2, 2], [3, 3], [5], [7]]
var项目=[[2]、[3]、[2,2]、[5]、[7]、[2,2,2]、[3,3]]
让reduced=items.sort({
设lhs=$0.first,rhs=$1.first
返回lhs==rhs?$0.count>1.count:lhs
或者,如果你想把所有这些都塞进一行:
var items = [[2], [3], [2, 2], [5], [7], [2, 2, 2], [3, 3]]
let reduced = items.sort({ let lhs = $0.first, rhs = $1.first; return lhs == rhs ? $0.count > $1.count : lhs < rhs }).reduce([[Int]]()) { $0.last?.last != $1.last ? $0 + [$1] : $0 }
print(reduced) // [[2, 2, 2], [3, 3], [5], [7]]
var项目=[[2]、[3]、[2,2]、[5]、[7]、[2,2,2]、[3,3]]
let reduced=items.sort({let lhs=$0.first,rhs=$1.first;return lhs==rhs?$0.count>$1.count:lhs
使用forEach只是一种替代方法:
let arrays = [[2], [2, 2], [5], [7], [2, 2, 2], [3, 3], [3]]
var largest: [Int: [Int]] = [:]
arrays.forEach({
guard let first = $0.first else { return }
largest[first] = [Int](count: max($0.count,largest[first]?.count ?? 0), repeatedValue: first)
})
Array(largest.values) // [[5], [7], [2, 2, 2], [3, 3]]
你得说得更具体些。您是否希望这是稳定的(保留原始顺序),应该如何处理重复项?我假设您只希望剩余的数组具有最高计数。顺序不重要,我已编辑了我的问题以使其更清楚。是,仅限计数最高的阵列。