If you want to get duplicates instead of uniq values in a list, how would you do this in a quick, dense script that uses pattern matching?
For example, with an input of ["ash", "bob", "cat", "bob", "ash"]
how could I get ["ash", "bob"]
?
Since you specified you wanted a quick, dense script, I think you should consider this solution:
l = ["ash", "bob", "cat", "bob", "ash", "ash"]
# to get all duplicates
l -- Enum.uniq(l) # => ["bob", "ash", "ash"]
# to get a unique list of duplicates
Enum.uniq(l -- Enum.uniq(l)) # => ["bob", "ash"]
Here is how I would do it:
["ash", "bob", "cat", "bob", "ash"]
|> (&((&1 -- (&1 |> Enum.uniq())) |> Enum.uniq())).()
This is the same as doing:
my_list = ["ash", "bob", "cat", "bob", "ash"]
(my_list -- (my_list |> Enum.uniq())) |> Enum.uniq()
What is happening:
my_list |> Enum.uniq()
Enum.uniq
to get these "duplicates" in unique list.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With