views:

34

answers:

2

Possible Duplicate:
removing duplicates in nsarray

Hi friends,

I am storing the value in nsarray and i have to remove the duplicate dates.i mean exactly like this (14/12/2010,paid,15/12/2010,pending,15/12/2010,pending,16/12/2010,paid) should be (14/12/2010,paid,15/12/2010,pending,16/12/2010,paid). can any one suggest me the best way for removing duplicates that has 2 different day in it.

Thanks in advance.

Regards, sathish

A: 

Use a NSSet (our NSMutableSet).

The objects in a set must respond to the NSObject protocol methods hash and isEqual: (see NSObject for more information). See the Collections Programming Topics

An other solution is to loop of each element of the array to add the element in an other array (mutable) if it is not duplicated:

NSMutableArray* filteredArray = [[NSMutableArray alloc]init];
for (id obj in array) {
  BOOL duplicated=NO;
  for (id objTest in filteredArray) {
    if ((obj.date==objTest.date) && (obj.status==objTest.status)) {
      duplicated = YES;
    }
  }
  if (duplicated==NO) {
    [filteredArray addObject:obj];
  }
}
Benoît
Can you explain how this would work with the only partially different items?
Eiko
Exact if object has more field...
Benoît
hi benoit data is on nsarray. it doesnot have key-value pair
sathish kumar
Adapt the code !
Benoît
A: 

You can use NSDictionary, with key = NSDate and value = status (paid/pending). When you insert any date in the dictionary, check dictionary's objectForKey: NSDate, if it returns nil, then you can insert the new date.

karim