Objective-C
NSMutableArray
Duplicate Removal
Programming
iOS Development

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

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

The best way to remove duplicates from an NSMutableArray depends on whether you need to preserve the original order. If order matters, NSOrderedSet is usually the cleanest solution. If order does not matter, converting through NSSet is simpler.

Decide First: Does Order Matter?

This is the most important design question.

  • If you only need unique values and do not care about ordering, a set-based conversion is fine.
  • If you want the first occurrence of each element to stay in the same relative order, use an ordered set or a manual scan.

A lot of short answers skip this distinction, but it changes which solution is actually correct.

Fastest Simple Deduping: NSSet

If ordering is irrelevant, convert the array to a set and then back to an array.

objective-c
1NSMutableArray *items = [NSMutableArray arrayWithArray:@[@"a", @"b", @"a", @"c"]];
2NSSet *uniqueSet = [NSSet setWithArray:items];
3NSMutableArray *uniqueItems = [NSMutableArray arrayWithArray:[uniqueSet allObjects]];
4
5NSLog(@"%@", uniqueItems);

This removes duplicates because a set cannot contain the same object twice.

The drawback is that sets do not preserve the original ordering, so uniqueItems may not come back in the order you expect.

Best General Answer When Order Matters: NSOrderedSet

If you want to keep the first occurrence order, NSOrderedSet is usually the best answer.

objective-c
1NSMutableArray *items = [NSMutableArray arrayWithArray:@[@"a", @"b", @"a", @"c"]];
2NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:items];
3NSMutableArray *uniqueItems = [NSMutableArray arrayWithArray:[orderedSet array]];
4
5NSLog(@"%@", uniqueItems);

This produces a deduplicated array while preserving order.

For most application code, this is the cleanest solution because it is concise and expresses the requirement directly.

Mutating The Existing Mutable Array

Sometimes you want to keep the same NSMutableArray instance and replace its contents.

objective-c
1NSMutableArray *items = [NSMutableArray arrayWithArray:@[@"a", @"b", @"a", @"c"]];
2NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:items];
3[items setArray:[orderedSet array]];
4
5NSLog(@"%@", items);

This is useful when the array is already shared with surrounding code and you want to update it in place rather than allocate a separate mutable array variable.

Manual Deduping For Custom Rules

If you need custom behavior, a manual loop is more flexible.

objective-c
1NSMutableArray *items = [NSMutableArray arrayWithArray:@[@"a", @"b", @"a", @"c"]];
2NSMutableArray *result = [NSMutableArray array];
3
4for (id obj in items) {
5    if (![result containsObject:obj]) {
6        [result addObject:obj];
7    }
8}
9
10NSLog(@"%@", result);

This preserves order, but it is less efficient than the set-based approaches for large arrays because containsObject: scans the current result repeatedly.

Still, the manual approach is valuable when equality is not the whole rule. For example, maybe you want to deduplicate by a single property of model objects rather than by object equality.

Deduplicating Custom Objects

Set-based approaches rely on Objective-C equality semantics. For Foundation collections, uniqueness depends on methods like isEqual: and hash.

If you store custom objects and want set-based deduping to behave correctly, make sure those methods are implemented consistently.

If you do not control the object's equality behavior, use a manual loop keyed by a specific property instead.

objective-c
1NSMutableArray *result = [NSMutableArray array];
2NSMutableSet *seenIDs = [NSMutableSet set];
3
4for (User *user in users) {
5    if (![seenIDs containsObject:user.userID]) {
6        [seenIDs addObject:user.userID];
7        [result addObject:user];
8    }
9}

This is often the real-world answer when deduplicating model arrays.

Performance Perspective

For large arrays, set-based deduping is generally better than repeatedly checking containsObject: in a growing result array.

A practical ranking is usually:

  • 'NSSet for uniqueness without order'
  • 'NSOrderedSet for uniqueness with order'
  • manual loop when you need custom dedupe rules

That is why NSOrderedSet is often the best balance for day-to-day app code.

Common Pitfalls

The most common mistake is using NSSet and then being surprised that the order changed. That is not a bug; sets are unordered.

Another mistake is assuming set-based deduping will work sensibly for custom objects without checking isEqual: and hash.

Developers also sometimes write a manual loop with containsObject: for very large arrays when an ordered set would be simpler and faster.

Finally, if you mutate the existing mutable array, be clear whether other parts of the code rely on that instance or expect a new array object.

Summary

  • Use NSSet when you want uniqueness and do not care about order.
  • Use NSOrderedSet when you want to preserve the original order.
  • Use [items setArray:...] if you want to update an existing NSMutableArray in place.
  • Use a manual loop when deduping depends on custom rules or object properties.
  • For most order-preserving cases, NSOrderedSet is the best general answer.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.