Search code examples
objective-cnsindexset

How to remove duplicates from NSIndexSet?


What's the best way to remove duplicate indices from an NSIndexSet? For example, say my NSIndexSet looks like this:

[ 1, 3, 3, 6, 7, 12, 12, 12 ]

I want it to look like:

[ 1, 3, 6, 7, 12 ]

What's the most efficient way to accomplish this?


Solution

  • NSIndexSet cannot possibly contain duplicates. What makes you think you have any?

    To be more specific, the documentation states

    The NSIndexSet class represents an immutable collection of unique unsigned integers, known as indexes because of the way they are used. This collection is referred to as an index set.

    The key point here is the word "unique", which means it cannot contain duplicates. Attempting to add an index to an index set that's already present will result in no change.