0

I am on XCode 8.2, OSX not iOS, Objective-C

I have an NSMutableArray with NSMutableArrays like this

@[
    @[@"value1",@"value2",@"value3",@"value4",@"value4"],
    @[@"value1",@"value2",@"value3",@"value4",@"value4"],
    @[@"value1",@"value2",@"value3",@"value4",@"value4"]
]

The values inside are completely dynamic. The above example is for demonstration. I need to filter these NSMutableArrays inside by multiple indexes. So - given indexes 1 and 3 the result should look like this

@[
    @[@"value2",@"value4"],
    @[@"value2",@"value4"],
    @[@"value2",@"value4"]
]

I already have a functional method with iterating over every array and removing the unneeded items at the corresponding indexes but this is very slow - so any recommendation to speed things up is welcome.

My current solution:

NSMutableIndexSet *indexes = [NSMutableIndexSet new];
[indexes addIndex:1];
[indexes addIndex:3];

for (NSMutableArray* theArray in allArrays)
    [theArray removeObjectsAtIndexes:indexes];
Pat_Morita
  • 3,355
  • 3
  • 25
  • 36
  • Possible duplicate of [Best way to remove from NSMutableArray while iterating?](http://stackoverflow.com/questions/111866/best-way-to-remove-from-nsmutablearray-while-iterating) – Ozgur Vatansever May 11 '17 at 09:03
  • and you might want to check [this answer](http://stackoverflow.com/a/126088/793428). – Ozgur Vatansever May 11 '17 at 09:04
  • `makeObjectsPerformSelector:withObject:` or `enumerateObjectsWithOptions:usingBlock:` might be faster. – Willeke May 11 '17 at 14:16
  • @OzgurVatansever This question is not a duplicate. The enumerated array isn't modified. Is the linked answer faster than `removeObjectsAtIndexes:`? – Willeke May 11 '17 at 14:22
  • @Willeke depending on your use case, yes, `removeObjectsAtIndexes` is considered to be the worst solution in terms of time complexity because for each deletion, it requires a lot of shifting operation to be made. – Ozgur Vatansever May 11 '17 at 15:43
  • @OzgurVatansever And in this case? – Willeke May 11 '17 at 22:50

0 Answers0