Can you elaborate on how you can derive a set from hashes by using values of True or 1? Might you have a link? Thanks.
Can you elaborate on how you can derive a set from hashes by using values of True or 1? Might you have a link? Thanks.
Sure. Np!
I mean that a simplified set is just a hash where the elements of the set are keys of the hash table and the values can be anything. I used 1 or True as example.
As in adding an element would be:
my_dict[element] = 1
Then membership check is: if element in my_dict
Then removal is deleting: del my_dict[element]
and so on.In other words, the reason sets are sometimes not explicitly there is because they are easy to implement on top of existing data structures.
Basic operations like union, difference, intersection between two sets can be done with a few simple for loops.
But like I mentioned in other comment, there is one interesting aspect to set (and hashes) in that the element now have to be hash-able. That kind of depends how mutability and identity works in the particular language.
The idea is that an item is in the set if it's in the hash table. You can add, remove, or test for membership in constant average time.
Set operations still need to be built on top of this.