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:

set = MapSet.new([1, 1, 1, 2])
MapSet.size(set)

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:

1..10 |> MapSet.new |> Enum.fetch(0)
{:ok, 1}
1..33 |> MapSet.new |> Enum.fetch(0)
{:ok, 11}

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.