views:

35

answers:

1

ruby code

irb(main):001:0> ary = ["a", "b", "b", "a", "b"]
irb(main):002:0> ary.uniq!

I want to write same code in objective-c.

+3  A: 

You use a NSSet to ensure uniqueness. setWithArray receives an array containing the objects to add to the new set. If the same object appears more than once in anArray, it is added only once to the returned set:

NSArray *arr = [[NSSet setWithArray: [NSArray arrayWithObjects: @"a", @"b", @"b", @"a", @"b", nil]] allObjects];
//If you want to obtain a mutable array:
NSMutableArray *mutArr = [NSMutableArray arrayWithArray: arr];

allObjects is used to return a NSArray representation of the NSSet, and this array contains all the unique objects in the initial array.

luvieere
I want to return NSMutableArray.
marcy
I edited to obtain a NSMutableArray
luvieere