Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiently search for NSString in a set

Consider a set of thousands of NSString objects, in memory.

What is the most efficient way to search for a particular NSString in the set? Would using NSDictionary suffice? Or is it guaranteed that NSSet's search is O(1) (couldn't find any documentation that says so)?

And would the same strategy apply to NSData objects?

like image 785
hpique Avatar asked Oct 06 '22 04:10

hpique


1 Answers

This page shows the following note about sets:

Note: If the objects in the set have a good hash function, accessing an element, setting an element, and removing an element all take constant time. With a poor hash function (one that causes frequent hash collisions), these operations take up to linear time. Classes such as NSString that are part of Foundation have a good hash function.

So for NSString you could expect constant time based on the above.

like image 123
dreamlax Avatar answered Oct 10 '22 04:10

dreamlax