How do I find to find which array is the longest (highest count) out of three arrays?
Background:
I have a matching function working well - three dictionaries with boolean values containg user preferences, an article has three tag categories, the function checks wether tag A is on in dictionary A, tag B is on in dictionary B, etc
Now the requirement is that there may be N entries in tag A, N entries in tag B, etc
So that each of the three arrays of tags could be different lengths, the easiest way I can think of is the find the longest array (with most entries) from ArrayA, ArrayB and ArrayC
This is my original working loop
for (id myArrayElement in storyArray) {
NSString *myString = [NSString stringWithString:[myArrayElement industryA]];
NSString *myIssue = [NSString stringWithString:[myArrayElement issueA]];
NSString *myService = [NSString stringWithString:[myArrayElement serviceA]];
if (
[prefsDictionary valueForKeyPath:[NSString stringWithFormat:@"Industries.%@", myString]] ||
[prefsDictionary valueForKeyPath:[NSString stringWithFormat:@"Issues.%@", myIssueElement]] ||
[prefsDictionary valueForKeyPath:[NSString stringWithFormat:@"Services.%@", myService]]
) {
// One of the story's tags matches a key in one of the corresponding dictionaries
// Look up what this preference is set to
NSString *keyvalue = [[prefsDictionary valueForKey:@"Industries"] valueForKey:myString];
NSString *Issuesvalue = [[prefsDictionary valueForKey:@"Issues"] valueForKey:myIssueElement];
NSString *Servicevalue = [[prefsDictionary valueForKey:@"Services"] valueForKey:myService];
if (
[keyvalue isEqualToString:@"1"] ||
[Issuesvalue isEqualToString:@"1"] ||
[Servicevalue isEqualToString:@"1"]
) {
// It's 开发者_如何学编程a match, add the story
[self.favList addObject:myArrayElement];
}
} // prefsDictionary End if
I'm thinking the best way to do this where the three inputs can be arrays of any length is
for (id myArrayElement in delegate.storyArray) {
NSArray *industyArr = [[myArrayElement industryA] componentsSeparatedByString:@"|"];
NSArray *issueArr = [[myArrayElement issueA] componentsSeparatedByString:@"|"];
NSArray *serviceArr = [[myArrayElement serviceA] componentsSeparatedByString:@"|"];
// We need to find longest array
// Pad the shorter arrays, or use if ([array count] >= 4) {id obj = [scores objectAtIndex:3];}
// Then loop using the largest array length
for (loop longest array length) {
// get nth entry in industyArr... thisIndustry
// get nth entry in issueArr... thisIssue
// get nth entry in serviceArr... thisService
if (
[prefsDictionary valueForKeyPath:[NSString stringWithFormat:@"Industries.%@", thisIndustry]] ||
[prefsDictionary valueForKeyPath:[NSString stringWithFormat:@"Issues.%@", thisIssue]] ||
[prefsDictionary valueForKeyPath:[NSString stringWithFormat:@"Services.%@", thisService]]
) {
// One of the story's tags matches a key in one of the corresponding dictionaries
NSString *keyvalue = [[prefsDictionary valueForKey:@"Industries"] valueForKey:thisIndustry];
NSString *Issuesvalue = [[prefsDictionary valueForKey:@"Issues"] valueForKey:thisIssue];
NSString *Servicevalue = [[prefsDictionary valueForKey:@"Services"] valueForKey:thisService];
if (
[keyvalue isEqualToString:@"1"] ||
[Issuesvalue isEqualToString:@"1"] ||
[Servicevalue isEqualToString:@"1"]
) {
// It's a match, add the story
[self.favList addObject:myArrayElement];
// EXIT THE INNER LOOP NOW WE HAVE A MATCH
}
} // prefsDictionary End if
} // End myIssueElement for
} // End myArrayElement for
Unless someone has an awesome idea...
If you're just trying to make sure that you look at all of the values in each array, I'd actually side with nielsbot. simply inserting
MAX(arrayA.count, MAX(arrayB.count, arrayC.count))
into your for loop parameter should cover it.
for( int i=0; i < MAX(arrayA.count, MAX(arrayB.count, arrayC.count)); i++ ) {
// Blah Blah Blah
}
MAX() returns the greater of two values, allowing you to easily pick out the highest count. From your example code, you don't seem to actually need the longest array, simply the highest count.
If all you want to do is find out which array has the highest count, this would be the approach I would take:
NSArray *largestArray = arrayA;
if ([largestArray count] < [arrayB count]) {
largestArray = arrayB;
}
if ([largestArray count] < [arrayC count]) {
largestArray = arrayC;
}
If there are many more arrays, this wouldn't be the optimal approach, but for just three, it should do just fine. This should return the largest array every time
精彩评论