Skip to content

Instantly share code, notes, and snippets.

@rcdilorenzo
Created July 25, 2012 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcdilorenzo/3177664 to your computer and use it in GitHub Desktop.
Save rcdilorenzo/3177664 to your computer and use it in GitHub Desktop.
Find Mode of Array in Obj C
NSMutableArray *cardRanks = [[NSMutableArray alloc] init];
for (LDNPlayingCard *card in self.cards) {
[cardRanks addObject:card.rank];
}
NSCountedSet *bagOfCardRanks = [[NSCountedSet alloc] initWithArray:cardRanks];
NSString *modeOfRanks = [[NSString alloc] init];
NSUInteger highestElementCount = 0;
for (NSString *rank in bagOfCardRanks) {
if ([bagOfCardRanks countForObject:rank] > highestElementCount) {
highestElementCount = [bagOfCardRanks countForObject:rank];
modeOfRanks = rank;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment