Confused
Confused

Reputation: 3926

Append arrays and remove duplicates without order change in objective c

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:

So I expect the result as like:

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

Question:

Thanks

Upvotes: 0

Views: 189

Answers (1)

marosoaie
marosoaie

Reputation: 2371

You can use NSMutableOrderedSet to achieve this:

NSMutableOrderedSet *mSet = [NSMutableOrderedSet new];
[mSet addObjectsFromArray:array1];
[mSet addObjectsFromArray:array2];
[mSet addObjectsFromArray:array3];
NSArray *array = [mSet array];

Upvotes: 3

Related Questions