0

Consider I have three arrays.

NSArray *array1 = @[@"4",@"3",@"2"];
NSArray *array2 = @[@"2",@"1"];
NSArray *array3 = @[@"3",@"1",@"5",@"2"];

I want to append these arrays. Conditions are:

  • Order should not change. Means, Array1's order has high priority than Array2.
  • Duplicate values should be removed (When appending Array2 with Array1, I want to remove duplicate values from Array2)

So I expect the result as like:

@[@"4",@"3",@"2",@"1",@"5"];

Question:

  • Should I iterate each and every values to construct the expected result? Is there any simple way to achieve it?

Thanks

1
  • shouldn't those arrays be mutable, or having an extra array in order to mutate them? Commented May 5, 2016 at 20:47

1 Answer 1

3

You can use NSMutableOrderedSet to achieve this:

NSMutableOrderedSet *mSet = [NSMutableOrderedSet new];
[mSet addObjectsFromArray:array1];
[mSet addObjectsFromArray:array2];
[mSet addObjectsFromArray:array3];
NSArray *array = [mSet array];
Sign up to request clarification or add additional context in comments.

Comments

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.