I am java programmer. There is concept of HashSet
in Java that don't add duplicate elements in a Set
. What is the similar concept in iPhone that do same ?
Please hint.
Thanks in advance.
I am java programmer. There is concept of HashSet
in Java that don't add duplicate elements in a Set
. What is the similar concept in iPhone that do same ?
Please hint.
Thanks in advance.
You can use NSSet in iOS. NSSet is a (unordered) collection of unique items. So, you can find the unique items in your array like so:
NSSet *uniqueElements = [NSSet setWithArray:myArray];
// iterate over the unique items
for(id element in uniqueElements)
{
// do something
}
NSSet most likely uses a hash algorithm to make insertion O(1) (compared to O(n^2) to check if each item is unique by iteration), but the Apple documentation does not make such a guarantee so you probably shouldn't count on that implementation detail.
If, for some reason you need to keep the unique items in a sorted (ordered) collection, you can turn the set back into an array with -[NSSet allObjects] and then sort the resulting array.
NSSet
use the has of NSObject
to make sure that there are not duplicates.
Or NSMutableSet
if you need to add thing to the set.
HashSet
is the Java implementation of a Hash Table. NSHashTable
is, I believe, the Cocoa equivalent.