Andres

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:

  1. 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.
  2. Is there a better way to get the first element of a MapSet?

Any suggestions to improve the algorithm is welcome.

Thanks.

Showing Posts 1 to 10

Qqwy

Qqwy

TypeCheck Core Team

In cases like this, where you check for a boolean result, if/else is usually used instead of case, so it’s good as is :slight_smile: .

I 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

Andres OP

Hi @Qqwy. Thanks for your response.

I don’t think there is, because there is no ‘first’ element in a MapSet.

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:

A set can contain any kind of elements, and elements in a set don’t have to be of the same type.

Thanks so much!

tty

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

peerreynders

Elements in Maps and MapSets are not ordered. So whichever element appears “first” is “random”, i.e. is implementation dependent.

mudasobwa

mudasobwa

Creator of Cure
use Bitwise
[1, 2, 3, 1, 3, 10, 200, 10, 200] |> Enum.reduce(0, &Bitwise.bxor/2)
#⇒ 2

:man_shrugging:

hauleth

hauleth

Classic methods are always the best one.

Andres

Andres OP

Hi @peerreynders.
Thank you very much for clarifying my question.

Andres

Andres OP

Hello @mudasobwa

This is amazing:

use Bitwise
[1, 2, 3, 1, 3, 10, 200, 10, 200] |> Enum.reduce(0, &Bitwise.bxor/2)
#⇒ 2

Thanks so much for sharing!

NobbZ

NobbZ

This will fail badly when someone puts a number thrice…

peerreynders

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 :slight_smile:

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews