Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir MapSet vs Enum.member?

Tags:

elixir

I need to keep only unique values and I am confused which data structure should I use. Later on I will only read the whole data structure.

  • One way is to use MapSet which will contain only unique elements.
  • The second approach is to check each time with Enum.member? whether an item is already there - but I can easily guarantee that through MapSet.

I am not sure which approach is better to use in such scenario. Which is more efficient, and which is better practice?

like image 458
Tano Avatar asked Feb 24 '26 13:02

Tano


1 Answers

If you keep your data in a MapSet, then Enum.member? will in fact call MapSet.member? underneath, so you will have all advantages of MapSet. You can see the relevant protocol implementation here.

like image 182
Paweł Obrok Avatar answered Feb 27 '26 08:02

Paweł Obrok