Search code examples
iosobjective-cnsmutablearraynsarraynsdictionary

find largest array using NSMustableArray of Arrays


I have an NSMutableArray of NSArrays, I would like some help finding the NSArray with the most items in it.

Initially I have an NSArray of NSDictionaries, there is a NSString called spaces in the NSDictionarys which is comma separated. I am reading all of these values into an NSArray then storing that NSArray into the NSMutableArray.

Here is that code. I am not sure if this is the best way to do that.

for (int m = 0; m < axisCount; m++) {
    NSDictionary *tempDictionary  = [infoArray objectAtIndex:m];
    NSDictionary *tempSymbolsDictionary = [tempDictionary objectForKey:@"Symbols"];
    NSDictionary *currentSymbolsDictionary = [tempSymbolsDictionary objectForKey:@"CurrentSymbols"];



 NSString *tempSpaceSymbolsString = [currentSymbolsDictionary objectForKey:@"SpaceSy"]; // looks something like 1;2;3;4;
    NSArray *newSpaceSysArray = [tempSpaceSymbolsString componentsSeparatedByString:@";"];
    [findSpacingSymbolsForPositionsMArray addObject:newSpaceSymbolsArray];

}

Solution

  • Since you build all the arrays sequentially, why not simply keep track of the largest one?

    NSArray *largestArray = nil;
    for (int m = 0; m < axisCount; m++) {
        // ...
        NSArray *newSpaceSymbolsArray = [tempSpaceSymbolsString componentsSeparatedByString:@";"];
        if (largestArray == nil || [newSpaceSymbolsArray count] > [largestArray count]) {
            largestArray = newSpaceSymbolsArray;
        }
    }