RosettaCodeData/Task/Averages-Mode/Objective-C/averages-mode.m

25 lines
589 B
Mathematica
Raw Permalink Normal View History

2013-04-10 22:43:41 -07:00
#import <Foundation/Foundation.h>
@interface NSArray (Mode)
- (NSArray *)mode;
@end
@implementation NSArray (Mode)
- (NSArray *)mode {
NSCountedSet *seen = [NSCountedSet setWithArray:self];
int max = 0;
NSMutableArray *maxElems = [NSMutableArray array];
2014-04-02 16:56:35 +00:00
for ( obj in seen ) {
2013-04-10 22:43:41 -07:00
int count = [seen countForObject:obj];
if (count > max) {
max = count;
[maxElems removeAllObjects];
[maxElems addObject:obj];
} else if (count == max) {
[maxElems addObject:obj];
}
}
return maxElems;
}
@end