Pergunta de entrevista da empresa Booking.com

Write a function to find elements that repeat a given N times in a given L list.

Respostas da entrevista

Sigiloso

25 de mar. de 2015

NSArray *mylist = @[@"Mohamed",@"Ali",@"Mohsen",@"Mohamed",@"Ali",@"Mohamed"]; NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:mylist]; for(NSNumber * item in countedSet) NSLog(@"%@ %u", item, [countedSet countForObject:item]);

1

Sigiloso

28 de dez. de 2017

- (id)returnElementWhichRepeat:(int)N intheList:(NSArray *)L{ NSCountedSet *countedSet = [[NSCountedSet alloc]initWithArray:L]; NSMutableArray *mArr = [[NSMutableArray alloc]initWithCapacity:L.count]; for (id value in countedSet) { if (N == [countedSet countForObject:value]) { [mArr addObject:value]; } } if ([mArr count]>0) { return mArr; } return nil; }