NSMutableArray Help!

Discussion in 'iOS Development' started by Old_Man_Willow, Jun 19, 2010.

  1. Old_Man_Willow

    Old_Man_Willow New Member

    Joined:
    Aug 7, 2009
    Messages:
    9
    Likes Received:
    0
    Device:
    iPod touch
    I'm not sure if I'm posting this in the right forum, but here's my question. I'm writing an app and I need to search an NSMutableArray for identical objects. However, what I need to know is not if there is an identical object or what it's index is, I need to know how many identical objects there are. Can someone please help?
  2. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    To make things clear, you want to know how many identical objects are in the array, as well as their indexes, and how many repetitions there are?

    Just want to make sure before I think about it

    Please Register or Log in to view images

  3. Old_Man_Willow

    Old_Man_Willow New Member

    Joined:
    Aug 7, 2009
    Messages:
    9
    Likes Received:
    0
    Device:
    iPod touch
    No, I only need to know the highest amount of repetitions. So if an array contains two identical Foo objects, three identical Fooer objects and four identical Fooest objects, the only result I need is the number 4.
  4. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    I'm not sure whether this is efficient enough for you, but it should work:
    [OBJC]NSCountedSet *set = [[NSCountedSet alloc] initWithArray: myArray];
    NSUInteger maxOccurrences = 0;
    for (id obj in [set objectEnumerator])
    {
    NSUInteger count = [set countForObject: obj];
    if (count > maxOccurrences) maxOccurrences = count;
    }
    [set release];[/OBJC]
    BTW: If you don't care about the order of objects in your array, you could might as well just use NSCountedSet instead of your mutable array for better performance.
  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    [OBJC]int arrayIndex = 0;
    for (int i = 0; i < [yourArray count]; i++) {

    id someObject = [yourArray objectAtindex:i];
    if ([someObject equals:theObjectYourComparingTo]) { //Where [object equals:] is a function you provide
    /* Do something */
    arrayIndex = i;
    break; //if necessary
    }

    }[/OBJC]

    EDIT: I didn't read your post. If you want the total amount of repetitions, then do this:

    [OBJC]int identicalObjectCount = 1;
    for (id someObject in yourArray) {

    if ([someObject equals:theObjectYourComparingTo]) { //Where [object equals:] is a function you provide
    /* Do something */
    identicalObjectCount++;
    }

    }[/OBJC]
  6. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    @Skylar:
    I think the problem (with your code) is that the object whose occurrences should be counted is unknown; the OP only wants the max repetition count. Thus, "your way" would require at least another hash table / dictionary to hold the counts for each reoccurring object.
  7. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    You can spend many countless hours dealing with sorting and organizing arrays, and inspecting arrays that I'm not going to even begin to try to attempt to provide the logic to the op. The basics of array iteration have been posted in this thread. Only the user can come up with the correct logic as it will be highly dependent on the application itself.

    Personally, I would create a helper object that contains the NSMutableArray as well as some other data. The helper object would automatically track and store the count of each object as a new object is added to the array, eliminating the need to go through and count when the application is trying to access the object counts.

    This would be memory inefficient for larger arrays and ridiculous overkill for smaller arrays. Only the op can figure out what is best.

Share This Page