Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Averages-Mode/Objective-C/averages-mode.m
Normal file
24
Task/Averages-Mode/Objective-C/averages-mode.m
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#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];
|
||||
for ( obj in seen ) {
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue