MapSet in Elixir
While writing some koans for MapSet I discovered some new information about it and thought I would share some of them here.
MapSet is another type of collection like lists and maps. What makes it unique is it doesn’t allow duplication.
If we did:
The result is 2.
The element 1 will only appear in there once and no more.
Ordered or unordered?
Another interesting thing about MapSets is that it is unordered which is true to a certain extent. MapSets are actually Hash array mapped trie. To a certain point it is ordered. In the case of Elixir MapSet it is up to 32 elements.
Example in IEx:
Performance
MapSet are really fast and is really useful when searching for something. MapSets can check existence of an element in O(log(n)) time. Here are some stats: MapSet evaluation.
Note that HashSet is deprecated in the later versions of Elixir
You may want to consider using a MapSet rather than a List when searching through a collection depending on your use case.