Python 有一个集合库。在我的 Web 应用程序中,我用来collections.Counter(aList).most_common([n])返回.naList
不错,简洁,相当快。
现在我正在开发一个 iOS 应用程序。Objective-C 或 Cocoa 框架中是否有任何东西可以为字符串数组提供相同的功能?
假设aList是一个NSArrary。NSStrings
NSMutableDictionary *aCounter = [NSMutableDictionary dictionaryWithCapacity:[aList count]];
for aString in aList {
    NSUInteger keyCount = [aCounter valueForKey:aString];
    if keyCount == nil {
        [aCounter setValue:1 forKey:aString];
    }
    else {
        [aCounter setValue:(keyCount+1) forKey:aString];
    }
有什么建议可以压缩它或提高它的性能吗?