PDA

View Full Version : Comparing Two Arrays




Ides
Aug 1, 2012, 04:12 PM
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:
Two arrays have equal contents if they each hold the same number of objects and objects at a given index in each array satisfy the isEqual: test.

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!



theyoda3
Aug 1, 2012, 06:33 PM
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, http://en.wikipedia.org/wiki/Sorting_algorithm
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.

IDMah
Aug 10, 2012, 12:04 PM
Look here (https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html)..

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 (http://stackoverflow.com/questions/9530494/sort-an-nsstring)

Good Luck.
Ian

chown33
Aug 10, 2012, 12:29 PM
The problem might also be solved using NSCountedSet, or maybe even NSIndexSet. It depends on exactly how the numbers are being used.

EnigMoiD
Aug 10, 2012, 01:27 PM
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.

Duncan C
Aug 10, 2012, 03:56 PM
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.

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