Use NSCountedSetand then find the largest 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;
}
}
Verification of the result:
NSLog(@"Most frequent string: %@", mostOccurring);
source
share