views:

66

answers:

3

Hi friends

how can i remove duplicates in nsarray. for instance my array contains following data. I want to compare with adjacent dates to avoid duplicates but it throughs error.

Can anyone guide me what i am going wrong

calendar first-

( 2010-09-25 17:00:00 GMT,

"AAA",
2010-09-25 17:00:00 GMT,
"AAA",
2010-09-26 17:00:00 GMT,
"BBB",
2010-09-26 17:00:00 GMT,
"BBB",
2010-09-27 17:00:00 GMT,
"CCCC",
2010-09-27 17:00:00 GMT,
"CCC",
2010-09-28 17:00:00 GMT,
"AAA",
2010-09-28 17:00:00 GMT,
"AAA",
2010-09-29 17:00:00 GMT,
"DDDD",
2010-09-29 17:00:00 GMT,
"DDDD",
2010-09-30 17:00:00 GMT,
"BBBB"
)

my code

NSArray dates; //dates contain above values

NSMutableArray *temp_date = [[NSMutableArray alloc] init];


for (int i=0; i<[dates count]; i+=2){
    BOOL day;


    if ([dates count]-2 >i) {


        day = [[dates objectAtIndex:i] compare:[dates objectAtIndex:i+2]];


    }   


    if (day) {
        [temp_date addObject:[dates objectAtIndex:i]];
        [temp_date addObject:[dates objectAtIndex:i+1] ];

    }



}

Regards, sathish

A: 

To find identical objects you should better use –indexOfObjectIdenticalTo: or –indexOfObjectIdenticalTo:inRange:

eviltrue
thanks eviltrue
sathish kumar
A: 

You could use sets. Duplicates will be removed automatically upon creation.

NSArray *cleanedArray = [[NSSet setWithArray:dates] allObjects];

Bear in mind that a set is unordered.

Joseph Tura
Joseph problem is it is dates is 2d array [[date1 - value],[date1 - value],[date2 - value]] I have remove duplicate date alone and and value corresponding should be as it is. I mean array has consective values(date,value,date,value etc..)
sathish kumar
Do you want to end up with just one value for each date? Or can one date be in the array multiple times, if the values are different?
Joseph Tura
Yes Joseph right- it should end up with one value. for example 2 dates has same value only we have remove one of it(but value can be duplicated it should not be removed)(i mean just union of dates alone in 2d array)
sathish kumar
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)
sathish kumar
How about wrapping those two values (date and status) in an object, putting that in the array and then removing duplicates with an NSSet?
Joseph Tura
but it contains many data that makes processing slow
sathish kumar
A: 

u could use isEqual method of NSObject . which compare the object content equality.

pawan.mangal