Comparing Two Arrays

Discussion in 'iOS Programming' started by Ides, Aug 1, 2012.

  1. Ides macrumors member

    Mar 27, 2012
    I'm trying to figure out how to tell if two NSArrays are equal to each other. I looked at apple's documentation for the instance method isEqualToArray: and it had this to say:
    This is a problem for me, because the arrays I'm using may contain the exact same objects, but in different order. In my app I have many NSArrays that each contain some NSNumber objects. I would like to compare two arrays to see if they each have the exact same numbers occurring the exact same amount of times, regardless of order.

    For example, I have one array with the numbers 5, 1, 3 (in that order). Another array has the numbers 3, 1, 5. I would consider these arrays equal for the purposes in my app because they both are the same, except for order. However, isEqualToArray: does take order into account, so I can't use it.

    Could someone suggest a way to tell if my two arrays are the same disregarding the order of the objects? Thanks in advance!
  2. theyoda3, Aug 1, 2012
    Last edited: Aug 1, 2012

    theyoda3 macrumors member

    Sep 27, 2006
    Are you new to programming?

    I do not use obj-C, but regardless of language here are two options that will work:

    1) Keep your arrays sorted or sort them before comparison. If you have positive integers and the range is not too large you could maybe use a radix, counting, or bucket sort. Otherwise you would have to use a comparison based sort like quick, merge, or heap sort. You can find more info about sorting here,
    2) Write your own comparison function. If you believe you do not know how, think about the problem for a while. Post a solution that you believe will work, even if it is slow, in pseudo code and ask for feedback on how to improve it.
  3. IDMah macrumors 6502


    May 13, 2011
    Look here ..

    You will still need to parse through each element.
    But I think copying the arrays to a tempArray and Sorting them seems like the best option.

    Here's a wacky solution.. keep a NSString as part of your object every time you add a number to the array add it to the string sort it and then string compare it .. thus keeping your orginal array in order..

    I said it was wacky didn't I.. shrug..

    sort string

    Good Luck.
  4. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    The problem might also be solved using NSCountedSet, or maybe even NSIndexSet. It depends on exactly how the numbers are being used.
  5. EnigMoiD macrumors member

    Aug 10, 2007
    Use NSSet instead of NSArray if order doesn't matter. [NSSet isEqualToSet:NSSet] does exactly what you're describing. You could go through the bother of making a category of NSArray to add this functionality, but it's not worth it.

    EDIT: Didn't see the post above me but it doesn't look like you would need any special kind of NSSet to do what the OP wants to do.
  6. Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia
    Converting the arrays to sets is by far the better way to go. Sorting arrays is slow, and sort time up with the number of elements being sorted by at least n Log(n) for a good sorting algorithm, or n squared for a bad algorithm.

    Converting to sets is pretty fast, and should happen in nearly linear time.

    If the arrays could contain multiple copies of the same object, and you need to make sure that the counts between arrays match, you should use NSCountedSet instead of NSIndexSet. (I'm not positive if the NSCountedSet version of isEqualToSet takes object counts into account. If it doesn't, you could add that check fairly easily by first doing an isEqualToSet test, and if that returns true, enumerating one NSCountedSet and comparing the count of each object to the count of the same object in the other set. If all counts match, you know both arrays contain the same number of instances of the exact same objects.)

Share This Page