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.
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.
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.
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.
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.
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.
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:
- '
NSSetfor uniqueness without order' - '
NSOrderedSetfor 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
NSSetwhen you want uniqueness and do not care about order. - Use
NSOrderedSetwhen you want to preserve the original order. - Use
[items setArray:...]if you want to update an existingNSMutableArrayin place. - Use a manual loop when deduping depends on custom rules or object properties.
- For most order-preserving cases,
NSOrderedSetis the best general answer.
Related reading
- The compiler is unable to type-check this expression Swift 4?
- The compiler is unable to type-check this expression Swift 4?
- The data couldn’t be read because it is missing error when decoding JSON in Swift
- The document Main.storyboard requires Xcode 8.0 or later
- the element type 'Listwidget' can't be assigned to the list type 'Widget
- The executable gets signed with invalid entitlements in Xcode
- The file MyApp.app couldn't be opened because you don't have permission to view it when running app in Xcode 6 Beta 4
- The iOS deployment target ''IPHONEOS_DEPLOYMENT_TARGET'' is set to 8.0, in Flutter How can I change the minimum IOS Deploying Target
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.