鉴于阵列NSString
S的有几个重复的副本:
AAA
BBB
AAA
AAA
BBB
BBB
BBB
BBB
CCC
什么是得到被大多数发生的字符串的最简单的方法?
鉴于阵列NSString
S的有几个重复的副本:
AAA
BBB
AAA
AAA
BBB
BBB
BBB
BBB
CCC
什么是得到被大多数发生的字符串的最简单的方法?
使用NSCountedSet
然后找到最大的countForObject:
NSCountedSet *bag = [[NSCountedSet alloc] initWithArray:myArray];
NSString *mostOccurring;
NSUInteger highest = 0;
for (NSString *s in bag)
{
if ([bag countForObject:s] > highest)
{
highest = [bag countForObject:s];
mostOccurring = s;
}
}
检查结果:
NSLog(@"Most frequent string: %@", mostOccurring);