Engaged Employer
Given an array of integers and integer K, return another array holding integers appeared K times in the given array
Anonymous
- (NSArray *)findElements:(NSArray*)array withK:(int)kTimes { NSMutableArray *resultArray = [NSMutableArray array]; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; for (NSNumber *number in array) { if ([dict objectForKey:number.stringValue]) { NSInteger count = [[dict objectForKey:number.stringValue] integerValue]; [dict setValue:[NSNumber numberWithInteger:count+1] forKey:number.stringValue]; } else { [dict setValue:[NSNumber numberWithInteger:1] forKey:number.stringValue]; } } for (NSString *key in dict) { NSInteger count = [[dict objectForKey:key] integerValue]; if (count == kTimes) { [resultArray addObject:key]; } } return resultArray; }
Check out your Company Bowl for anonymous work chats.