2

I have 2 NSArray's that are holding values...

For example NSArray 1 has values 1 2 4 in it

and NSArray 2 has values 1 2 4 5 6 in it.

How can I write code to compare these 2 arrays to get the following information...

Count the values that are the same (so in this case 3) and count the values that are not the same (in this case 2).

I am simply populating the arrays like this: NSString *s = @"1,2,4"; NSArray *numbers = [s componentsSeparatedByString:@","];

where *s is actually getting the text from a UITextField. If sorting mattering in comparing can you show me code to sort to make sure the user doesnt put the numbers in order?

3
  • 1
    this is rather straightforward, what have you tried, what didn't work? Commented Jun 28, 2010 at 14:54
  • You can take a look at this SO entry, as it contains half of the solution. For the remaining half, you should be able to figure how to do it. Commented Jun 28, 2010 at 14:55
  • Are the arrays sorted? Are duplicates allowed? Commented Jun 28, 2010 at 14:56

2 Answers 2

2

If you are fine with sets instead of arrays, you can use NSMutableSet instead of NSArray. NSMutableSet has nice methods like intersectSet: and minusSet:

Sign up to request clarification or add additional context in comments.

Comments

1

I would probably use the following method of the NSArray class:

enumerateObjectsUsingBlock.

and code the block testing for membership in the other array with the method:

indexOfObjectIdenticalTo.

If this isn't clear to you let me know.

1 Comment

Im not very comfortable with odjective c yet can you write out an example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.