I need a datastructure that has O(1) insert and remove operations and from which I can retrieve a random element at O(1).
I thought of using HashSet in C#. Insert and remove operations are constant and I would be able to also get a random element from it if I could access the internal array. I don't want to create an array from the set, that would take O(n) every time.
So my question is, is there a way to access the internal array of a HashSet? Or is there a better datastructure that meets the requirements?
Note: for example a Dictionary<TKey, TValue> allows to access the internal array for the keys and the values. It would be awesome if a HashSet had this too. I could use a Dictionary, but I wouldn't really use it for the mapping feature, which requires twice as much space as a HashSet.