查找哪个数组的计数最高

发布于 2024-11-06 19:30:15 字数 3706 浏览 2 评论 0原文

如何找到三个数组中最长(最高计数)的数组?

背景:

我有一个运行良好的匹配函数 - 三个带有布尔值的字典包含用户首选项,一篇文章有​​三个标签类别,该函数检查标签 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

除非有人有一个很棒的想法......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离不开的别离 2024-11-13 19:30:15

如果您只是想确保查看每个数组中的所有值,我实际上会站在 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() 返回两个值中的较大者,使您可以轻松选出最高计数。从您的示例代码来看,您似乎实际上并不需要最长的数组,而只需要最高的计数。

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.

私野 2024-11-13 19:30:15

如果您想做的只是找出哪个数组具有最高计数,这将是我会采取的方法:

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

如果有更多数组,这不是最佳方法,但对于只有三个数组,它应该做美好的。这应该每次都返回最大的数组

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文