Ajaxdone

Ajaxdone

How to check if a list is a sublist(out of order list) of another list

l1 = ["1","2","5","9"]
l2 = ["1","9"]
List.contains?(l1,l2)

Anything like this? to check if l2 is a sublist of l1.

I know can use Enum.each and check one by one, just want to know if anything like that. thanks

Marked As Solved

emoragaf

emoragaf

maybe using MapSet.subset?/2

Also Liked

NobbZ

NobbZ

l2 is not a sublist of l1.

But in general, sublist checking is quite easy to do.

def sublist?([], _), do: false
def sublist?(l1 = [_|t], l2) do
  List.starts_with?(l1, l2) or sublist?(t, l2)
end
hauleth

hauleth

def subset(_, []), do: true
def subset([], _), do: false
def subset([h | t1], [h | t2]), do: subset(t1, t2)
def subset([_ | t], list), do: subset(t, list)
Ajaxdone

Ajaxdone

According to @NobbZ 's suggestion as well, I tried this one works:

big_list = ["9", "2", "5", "1"]
sub_out_of_order_list = ["1", "9"]
MapSet.subset?(MapSet.new(sub_out_of_order_list), MapSet.new(big_list))

Thank you everyone.

gregvaughn

gregvaughn

Depending on the relative sizes of your lists, this could also be a viable option:

Enum.all?(sub_out_of_order_list, &Enum.member?(big_list, &1))

Ajaxdone

Ajaxdone

Maybe I need to clarify a bit, actually I don’t mean sublist, I mean a list’s all items in another list, don’t care order.

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement