如何计算Swift数组中元素的出现次数?

我已经看到了一些这样的示例,但是所有这些似乎都依赖于知道要计算发生次数的元素。我的数组是动态生成的,所以我无法知道要计算哪个元素的出现(我想计算所有元素的出现)。有人可以建议吗?

提前致谢

编辑:

也许我应该更清楚一点,数组将包含多个不同的字符串(例如 ["FOO", "FOO", "BAR", "FOOBAR"]

我如何在不知道它们是什么的情况下计算foo,bar和foobar的出现?


森林海
浏览 1098回答 3
3回答

阿晨1998

使用Swift 5时,您可以根据需要选择以下7个Playground示例代码之一来计算数组中可哈希项的出现次数。#1。使用Array的reduce(into:_:)和Dictionary的subscript(_:default:)标let array = [4, 23, 97, 97, 97, 23]let dictionary = array.reduce(into: [:]) { counts, number in&nbsp; &nbsp; counts[number, default: 0] += 1}print(dictionary) // [4: 1, 23: 2, 97: 3]#2。使用repeatElement(_:count:)函数,zip(_:_:)函数和Dictionary的init(_:uniquingKeysWith:)初始值设定项let array = [4, 23, 97, 97, 97, 23]let repeated = repeatElement(1, count: array.count)//let repeated = Array(repeating: 1, count: array.count) // also workslet zipSequence = zip(array, repeated)let dictionary = Dictionary(zipSequence, uniquingKeysWith: { (current, new) in&nbsp; &nbsp; return current + new})//let dictionary = Dictionary(zipSequence, uniquingKeysWith: +) // also worksprint(dictionary) // prints [4: 1, 23: 2, 97: 3]#3。使用Dictionary的init(grouping:by:)初始值设定项和mapValues(_:)方法let array = [4, 23, 97, 97, 97, 23]let dictionary = Dictionary(grouping: array, by: { $0 })let newDictionary = dictionary.mapValues { (value: [Int]) in&nbsp; &nbsp; return value.count}print(newDictionary) // prints: [97: 3, 23: 2, 4: 1]#4。使用Dictionary的init(grouping:by:)初始值设定项和map(_:)方法let array = [4, 23, 97, 97, 97, 23]let dictionary = Dictionary(grouping: array, by: { $0 })let newArray = dictionary.map { (key: Int, value: [Int]) in&nbsp; &nbsp; return (key, value.count)}print(newArray) // prints: [(4, 1), (23, 2), (97, 3)]#5。使用for循环和Dictionary的subscript(_:)下标extension Array where Element: Hashable {&nbsp; &nbsp; func countForElements() -> [Element: Int] {&nbsp; &nbsp; &nbsp; &nbsp; var counts = [Element: Int]()&nbsp; &nbsp; &nbsp; &nbsp; for element in self {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counts[element] = (counts[element] ?? 0) + 1&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return counts&nbsp; &nbsp; }}let array = [4, 23, 97, 97, 97, 23]print(array.countForElements()) // prints [4: 1, 23: 2, 97: 3]#6。使用NSCountedSet和NSEnumerator的map(_:)方法(需要Foundation)import Foundationextension Array where Element: Hashable {&nbsp; &nbsp; func countForElements() -> [(Element, Int)] {&nbsp; &nbsp; &nbsp; &nbsp; let countedSet = NSCountedSet(array: self)&nbsp; &nbsp; &nbsp; &nbsp; let res = countedSet.objectEnumerator().map { (object: Any) -> (Element, Int) in&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (object as! Element, countedSet.count(for: object))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return res&nbsp; &nbsp; }}let array = [4, 23, 97, 97, 97, 23]print(array.countForElements()) // prints [(97, 3), (4, 1), (23, 2)]#7。使用NSCountedSet和AnyIterator(需要Foundation)import Foundationextension Array where Element: Hashable {&nbsp; &nbsp; func counForElements() -> Array<(Element, Int)> {&nbsp; &nbsp; &nbsp; &nbsp; let countedSet = NSCountedSet(array: self)&nbsp; &nbsp; &nbsp; &nbsp; var countedSetIterator = countedSet.objectEnumerator().makeIterator()&nbsp; &nbsp; &nbsp; &nbsp; let anyIterator = AnyIterator<(Element, Int)> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guard let element = countedSetIterator.next() as? Element else { return nil }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (element, countedSet.count(for: element))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return Array<(Element, Int)>(anyIterator)&nbsp; &nbsp; }}let array = [4, 23, 97, 97, 97, 23]print(array.counForElements()) // [(97, 3), (4, 1), (23, 2)]
打开App,查看更多内容
随时随地看视频慕课网APP