I have an array of strings from an online database that I trying to determine the most commonly used word. The values inside the arrays will vary but I want to check the most common words of whatever collection or words I'm using. If theoretically I had an array of the following...
NSArray *stringArray = [NSArray arrayWithObjects:@"Duck", @"Duck", @"Duck", @"Duck", @"Goose"];
How do I iterate through this array to determine the most common string, which would obviously be "Duck"?
Try using NSPredicate.
Simplest way is probably
NSCountedSet
:You can use the word as a key into a dictionary.
Now iterate through
words
and find the key with the highest value.EDIT: Rather than looping through the array once then looping separately through the sorted dictionary, I think we can do better and do it all in a single loop.
This should be significantly faster than my answer pre-edit, though I don't know how it compares to using the
NSCountedSet
answer.