2011-05-11 36 views
1

如何查找哪個數組是三個數組中最長的(最高計數)?查找哪個數組的計數最高

背景:

我具有匹配功能運作良好 - 3個詞典與方含用戶偏好布爾值,製品具有三個標籤類,函數檢查閹標籤A是在字典A,標籤B是上在字典B,等等

現在的要求是,有可能在標籤A N個條目,在標籤B N個條目,等等

使每個標籤的三個陣列中可以是不同的長度,最簡單的我能想到的方式是從ArrayA,ArrayB和ArrayC中找到最長的數組(大多數條目)

這是我原來的工作循環

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 

我想做到這一點,其中三個輸入可以是任意長度的陣列,最好的辦法是

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 

除非有人有一個真棒想法...

+0

我明顯錯過了你要找的東西,因爲'[arrayX count]'給你數組中元素的數量。 – 2011-05-11 23:18:31

+0

抱歉 - 試圖更好地理解問題。你正在搜索文章的集合,以找到哪些標籤用戶的標籤? – nielsbot 2011-05-11 23:23:39

+0

@neilsbot我想找到最長的陣列選擇陣列。這將允許我的邏輯工作。我認爲代碼塊可能對某人有用,並且在發佈時,有人可能會說......你應該這樣做...... – JulianB 2011-05-12 00:11:12

回答

1

如果您只是想確保您查看每個陣列中的所有值,我實際上會與nielsbot一起。簡單地插入

MAX(arrayA.count, MAX(arrayB.count, arrayC.count)) 

到您的for循環參數應該覆蓋它。

for(int i=0; i < MAX(arrayA.count, MAX(arrayB.count, arrayC.count)); i++) { 
    // Blah Blah Blah 
} 

MAX()返回兩個值中的較大者,讓您輕鬆挑選出最高計數。從您的示例代碼中,您似乎並不需要最長的數組,而只需要最高的數。

+0

其實,你是對的。我已經重讀了你的解決方案,這很有道理。奧克曼剃刀等等。也謝謝你! – JulianB 2011-05-12 00:15:48

0

如果你想要做的是找出哪些陣列具有最高的計數,這將是我的做法會採取:

NSArray *largestArray = arrayA; 
if ([largestArray count] < [arrayB count]) { 
    largestArray = arrayB; 
} 
if ([largestArray count] < [arrayC count]) { 
    largestArray = arrayC; 
} 

如果有更多的數組,這不會是最佳的方法,但只有三個,它應該做得很好。這應該每次都會返回最大的陣列

+1

甚至是MAX(arrayA.count,MAX(arrayB.count,arrayC)。計數))' – nielsbot 2011-05-11 23:20:25

+0

哦對不起 - 這只是最高的數量,而不是最高的數組。 – nielsbot 2011-05-11 23:20:57

+0

是很好的解決方案,謝謝。它甚至給我變量用於我的循環,swcheeeet。我希望這是一些魔術,就像我不知道的猜測,但不要猜測 – JulianB 2011-05-12 00:12:57