The Best Way to Remove Duplicate Values from Nsmutablearray in Objective-C

The best way to remove duplicate values from NSMutableArray in Objective-C?

Your NSSet approach is the best if you're not worried about the order of the objects, but then again, if you're not worried about the order, then why aren't you storing them in an NSSet to begin with?

I wrote the answer below in 2009; in 2011, Apple added NSOrderedSet to iOS 5 and Mac OS X 10.7. What had been an algorithm is now two lines of code:

NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:yourArray];
NSArray *arrayWithoutDuplicates = [orderedSet array];

If you are worried about the order and you're running on iOS 4 or earlier, loop over a copy of the array:

NSArray *copy = [mutableArray copy];
NSInteger index = [copy count] - 1;
for (id object in [copy reverseObjectEnumerator]) {
if ([mutableArray indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound) {
[mutableArray removeObjectAtIndex:index];
[copy release];

Removing duplicates from array based on a property in Objective-C

You might have to actually write this filtering method yourself:

@interface NSArray (CustomFiltering)

@implementation NSArray (CustomFiltering)

- (NSArray *) filterObjectsByKey:(NSString *) key {
NSMutableSet *tempValues = [[NSMutableSet alloc] init];
NSMutableArray *ret = [NSMutableArray array];
for(id obj in self) {
if(! [tempValues containsObject:[obj valueForKey:key]]) {
[tempValues addObject:[obj valueForKey:key]];
[ret addObject:obj];
[tempValues release];
return ret;


Best way to remove duplicate objects from multi dimension MutableArray objective-C

You can use the property of NSSets that they will only store a single instance of a given value.

  • First convert each sub-array into a set, that will remove duplicates that are in that array.

  • Then, subtract the set of items that have already been seen.

  • Convert the result back to an array and add it to your output array.

  • Finally, add the items in that sub-array to the set of items that have already been seen.

Something like:

-(NSArray *)removeDuplicatesFromArray:(NSArray *)array {

NSMutableArray *returnArray=[NSMutableArray new];
NSMutableSet *cumulativeSet=[NSMutableSet new];

for (NSArray *innerArray in array) {
NSMutableSet *innerSet = [NSMutableSet setWithArray:innerArray];
[innerSet minusSet:cumulativeSet];
[cumulativeSet unionSet:innerSet];

[returnArray addObject:[innerSet allObjects]];

return [returnArray copy];

NSMutableArray - Remove duplicates

Your method should work, except it returns an NSArray instead of NSMutableArray. You could use

[values setArray:[[NSSet setWithArray:values] allObjects]];

to set values to the content of the new array.

How to remove duplicate of objects that has same value from NSArray

Just use following code for remove duplicates values.

your_array = [self groupsWithDuplicatesRemoved:(NSArray *)your_array myKeyParameter:@"your_key_name"];

You have to just call groupsWithDuplicatesRemoved this method with key name.

- (NSMutableArray *) groupsWithDuplicatesRemoved:(NSArray *)  groups myKeyParameter:(NSString *)myKeyParameter {
NSMutableArray * groupsFiltered = [[NSMutableArray alloc] init]; //This will be the array of groups you need
NSMutableArray * groupNamesEncountered = [[NSMutableArray alloc] init]; //This is an array of group names seen so far

NSString * name; //Preallocation of group name
for (NSDictionary * group in groups) { //Iterate through all groups
name = [NSString stringWithFormat:@"%@", [group objectForKey:myKeyParameter]]; //Get the group name
if ([groupNamesEncountered indexOfObject: name]==NSNotFound) { //Check if this group name hasn't been encountered before
[groupNamesEncountered addObject:name]; //Now you've encountered it, so add it to the list of encountered names
[groupsFiltered addObject:group]; //And add the group to the list, as this is the first time it's encountered
return groupsFiltered;

Hope, this is what you're looking for. Any concern get back to me. :)

Objective-C: Remove duplicates efficiently from large data structures

It is not completely clear to me what your problem is, maybe this will help:

A good way to remove duplicates is not to add them in the first place, replace:

for(NSNumber *resultId in resultsIndexArray)
[titlesArray addObject:indexDictionary];
[resultArray addObject:resultId];


for(NSNumber *resultId in resultsIndexArray)
// only add if resultId not already in resultArray
if( ![resultArray containsObject:resultId] )
[titlesArray addObject:indexDictionary];
[resultArray addObject:resultId];

The containsObject: call requires a linear search, if your data set is large you might wish to change resultArray to an NSMutableSet and titlesArray to an NSMutableDictionary mapping from resultId to indexDictionary values.


Remove duplicates from large NSMutableArray

What about:

NSArray *arrayWithNoDuplicates = [[NSSet setWithArray:papersObject.paperSubject] allObjects];

Remove Duplicate Objects From NSMutableArray object property?

Here's one solution:

NSArray *originalArray = ... // original array of objects with duplicates
NSMutableArray *uniqueArray = [NSMutableArray array];
NSMutableSet *names = [NSMutableSet set];
for (id obj in originalArray) {
NSString *destinationName = [obj destinationname];
if (![names containsObject:destinationName]) {
[uniqueArray addObject:obj];
[names addObject:destinationName];

remove duplicate valuse from NSMutable array

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]) {
[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; }

Related Topics

Leave a reply