I have an NSMutableArray that I display through a table view. The problem is that when I add objects to this array, I don’t want to have duplicates.
If I create an NSMutableSet from the NSMutableArray, then add objects to the NSMutableSet and then convert it back to an NSMutableArray, is that any more efficient than checking the NSMutableArray through a loop for duplicates before adding an item?
Generally yes, it would be more efficient to use a set. Constructing a set of n items is
O(n log n). Finding all the duplicates in an array by just looping through it will beO(n^2). (If you’re really determined you could get O(n log n), but you’d have to kinda rewrite what set already does.)