Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4618444
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:16:03+00:00 2026-05-22T02:16:03+00:00

How do I find to find which array is the longest (highest count) out

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:16:04+00:00Added an answer on May 22, 2026 at 2:16 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large array and would like to find between which array values
How do I find an item in array which has the most occurrences? [1,
How can I find out which Service Pack is installed on my copy of
How can I find out which node in a tree list the context menu
How do I find out which process is listening on a TCP or UDP
How do I find out which sound files the user has configured in the
How can I find out which column and value is violating the constraint? The
Am I missing something in the Array documentation? I have an array which contains
I have four DIV elements on my page. Each has it's own array which
I want to find out which user is not part of other games available

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.