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

If you are targeting iOS 5+ (what covers the whole iOS world), best use NSOrderedSet. It removes duplicates and retains the order of your NSArray.

Just do

NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:yourArray];

You can now convert it back to a unique NSArray

NSArray *uniqueArray = orderedSet.array;

Or just use the orderedSet because it has the same methods like an NSArray like objectAtIndex:, firstObject and so on.

A membership check with contains is even faster on the NSOrderedSet than it would be on an NSArray

For more checkout the NSOrderedSet Reference

Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to objective-c

Adding a UISegmentedControl to UITableView Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Warp \ bend effect on a UIView? Use NSInteger as array index Detect if the device is iPhone X Linker Command failed with exit code 1 (use -v to see invocation), Xcode 8, Swift 3 ITSAppUsesNonExemptEncryption export compliance while internal testing? How to enable back/left swipe gesture in UINavigationController after setting leftBarButtonItem? Change status bar text color to light in iOS 9 with Objective-C

Examples related to nsmutablearray

'Invalid update: invalid number of rows in section 0 How do I convert NSMutableArray to NSArray? The best way to remove duplicate values from NSMutableArray in Objective-C? How do I sort an NSMutableArray with custom objects in it?