1

i have a nsmutablearray as is shown below. I would like to retrieve list value without redundancy. in this example i should retrive 10 20 30

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"10",@"20",@"30",@"30",@"30",@"30",@"20", nil];
BERGUIGA Mohamed Amine
  • 6,094
  • 3
  • 40
  • 38

4 Answers4

3

Transform the array into a set, and then back into an array.

NSSet *set = [NSSet setWithArray:array];

NSArray *a = [set allObjects];

You can also have this new array sorted:

NSArray *a2 = [a sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    return [obj1 compare:obj2];
}];

Since iOS 5 you can use -[NSOrderedSet orderedSetWithArray:] which preserves the order:

NSArray *a2 = [[NSOrderedSet orderedSetWithArray:array] array];
nst
  • 3,862
  • 1
  • 31
  • 40
1
NSArray *withRedunecy = [[NSSet setWithArray: array] allObjects];

This will be the one way like you can create new NSArray which has no duplicate objects or you need to create a logic for getting unique objects like insert one by one with checking is it already present or not.

Buntylm
  • 7,345
  • 1
  • 31
  • 51
1

Try to use this on

NSArray *array = @[@"10",@"20",@"30",@"30",@"30",@"30",@"20"];

NSArray *newArray =  [[NSSet setWithArray:array] allObjects];
NSLog(@"%@", newArray);  

Output : ( 30, 20, 10 )

Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

Only this line of code will work fine .

NSSet *mySet = [NSSet setWithArray:array];

now mySet will have unique elements.so create array with this set

NSArray *myarray = [mySet allObjects];
Sumit Mundra
  • 3,891
  • 16
  • 29