Andres
Hello.
Trying to find the only element that does not appear twice I wrote the following algorithm:
list = [1, 2, 3, 1, 3, 10, 200, 10, 200] # Expected result => 2
def single_one(list) do
list
|> Enum.reduce(%MapSet{}, fn x, acc ->
if MapSet.member?(acc, x) do
MapSet.delete(acc, x)
else
MapSet.put(acc, x)
end
end)
|> MapSet.to_list()
|> hd()
end
I have the following questions:
- I would like to know if in Elixir it is good practice to use if else as I did in the previous algorithm.
I could not find a way to access the first and only element of the MapSet.
I proceeded to convert the MapSet into a list and then its head. - Is there a better way to get the first element of a MapSet?
Any suggestions to improve the algorithm is welcome.
Thanks.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Qqwy
In cases like this, where you check for a boolean result,
.
if/elseis usually used instead ofcase, so it’s good as isI don’t think there is, because there is no ‘first’ element in a MapSet. In this case, you know that there is only a single one, but in the more general case, elements inside a MapSet are not ordered. So if you have more than a single element in a set, there is no idea which element you’d obtain when extracting a single one from it.
Andres
Hi @Qqwy. Thanks for your response.
Could you be so kind to explain me how I can refer to the first element of a MapSet? Or what do you mean there is not a first element in a MapSet?
The official definition of MapSet says the following:
Thanks so much!
tty
I would suggest something similar to a counting sort. That would only take O(2n) if you know the range O(3n) if you don’t.
peerreynders
Elements in Maps and MapSets are not ordered. So whichever element appears “first” is “random”, i.e. is implementation dependent.
mudasobwa
hauleth
Classic methods are always the best one.
Andres
Hi @peerreynders.
Thank you very much for clarifying my question.
Andres
Hello @mudasobwa
This is amazing:
Thanks so much for sharing!
NobbZ
This will fail badly when someone puts a number thrice…
peerreynders
Find the element that appears once in an array where every other element appears twice
Feels like a solution that was looking for a problem statement