I require a data structure that is dynamic (Add/Remove) and that is the most efficient for
Contains()
Iteration
Both actions are called more often than (add/remove)
What about a combination from a List and a HashSet?
I require a data structure that is dynamic (Add/Remove) and that is the most efficient for
Contains()
Iteration
Both actions are called more often than (add/remove)
What about a combination from a List and a HashSet?
As mentioned in the comments by @Ross Gurbutt the HashSet satisfies your requirements. It does not, however, maintain the order of items inserted into it (or more precisely, it might not. See this answer by Jon Skeet).
If the fidelity of the order is important to you, then the SortedSet might be what you're looking for.
Edit: The above recommendations go on the assumption that you do not want to allow for duplicates in your collection(s).
Also, as @Ross Gurbutt kindly pointed out (and as I should have mentioned originally):
SortedSet.Contains is O(log n) whereas HashSet.Contains is O(1)
You can use more than one data structure.
Use a HashSet
to get an O(1) access time to any element.
Use a List<>
to add/remove items by index or to the end of the list: O(1).
That List
can also be searched by a binary search if it's ordered: O(log(n)). If it is not ordered, that search is linear, O(n).
Iteration is iteration: always O(n)... in this case over your List
.