Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove duplicate valuse from NSMutable array [duplicate]

I am trying to remove any duplicates from my sorted array... I thought I could do it using an example from another question of mine but it turns out to not even come close to working.

its a pretty horrible attempt, but its the only way i can figure out how to compare two elements of a single item(NSDictionary) in a NSMutableArray.

Here is my code example

I currently have a sorted array

 NSArray *duplicatesRemovedArray = [sortedArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
        NSDictionary *dictA = a;
        NSDictionary *dictB = b;

        NSString *startYear1 = dictA[kStartYear];
        NSString *finishYear1 = dictA[kFinishYear];
        NSString *allYear1 = [NSString stringWithFormat:@"%@%@",startYear1, finishYear1];
        NSString *startYear2 = dictB[kStartYear];
        NSString *finishYear2 = dictB[kFinishYear];
        NSString *allYear2 = [NSString stringWithFormat:@"%@%@",startYear2, finishYear2];

        return [allYear1 compare:allYear2];

    }];

if anyone has any ideas on how to create an NSArray of unique items that is what I am after, if you could give me any suggestions, code examples that would be greatly appreciated

Update: I should point out that my NSDictionary has another element within it an ID...

so I cannot just compare objects like in the examples you have listed... this was an oversight on my part. So effectivly the objects look like this

ID: 1234
STARTYEAR: 1999
FINISHYEAR: 2000

so I need some help removing objects whos StartYear and FinishYear match other objects start and finish year.

like image 424
C.Johns Avatar asked Jul 18 '26 16:07

C.Johns


1 Answers

So if I understood your question correctly, you want to filter your array in a way that if there's more than one item with the same year values (but possibly different IDs), only one of them is contained in the result.

A simple way to do that would be to iterate over the array, collecting start/finish years that you've already seen in a mutable set, and only including items in the result if they have a combination of start/finish year that you haven't yet added.

NSArray *array = @[@{@"ID": @(1234), @"STARTYEAR": @(1999), @"FINISHYEAR": @(2001)},
                   @{@"ID": @(1235), @"STARTYEAR": @(1999), @"FINISHYEAR": @(2001)},
                   @{@"ID": @(1236), @"STARTYEAR": @(1999), @"FINISHYEAR": @(2000)},
                   @{@"ID": @(1237), @"STARTYEAR": @(1999), @"FINISHYEAR": @(2000)}];

NSMutableArray *duplicatesRemoved = [NSMutableArray array];
NSMutableSet *seenYears = [NSMutableSet set];
for (NSDictionary *item in array) {
    //Extract the part of the dictionary that you want to be unique:
    NSDictionary *yearDict = [item dictionaryWithValuesForKeys:@[@"STARTYEAR", @"FINISHYEAR"]];
    if ([seenYears containsObject:yearDict]) {
        continue;
    }
    [seenYears addObject:yearDict];
    [duplicatesRemoved addObject:item];
}
NSLog(@"%@", duplicatesRemoved);

In this example, this would result in these two items:

{ FINISHYEAR = 2001; ID = 1234; STARTYEAR = 1999; }
{ FINISHYEAR = 2000; ID = 1236; STARTYEAR = 1999; }
like image 78
omz Avatar answered Jul 20 '26 19:07

omz