# Check if two elements exist in a list

I would like to know how i can determine if two elements in a list exist. For example the setup would look like this:

liste = {"b", "d1", "a","z"}
MemberQ[{{"b", "a"}}, liste]


One problem i have is that it should not depend on the order of the two element, i.e. {"b", "a"} == {"a", "b"} that i want to check. I just want to know if these exist in a given list.

• Hello, have a look at the documentation for ContainsAll , cheers! Nov 16, 2021 at 19:57
• Intersection can do this: Intersection[liste, {"b", "a"}] != {} Nov 17, 2021 at 11:25

list = {"b", "d1", "a", "z"};

ab = {"a", "b"};

ContainsAll[list, ab]


True

SubsetQ[list, ab]


True

Last @ UniqueElements[{list, ab}] == {}


True

list = {"b", "d1", "a", "z"};

ab = {"a", "e"};


An alternative is the following:

Thread[# -> Thread[Lookup[Counts[Catenate[{list, #}]], #] &@# > 1]] &@ab

(*{"a" -> True, "e" -> False}*)


Using OrderlessPatternSequence:

Clear["Global*"];

f[k_List, elems_List] :=
If[Cases[k, {OrderlessPatternSequence[Sequence @@ elems, ___]},
All] == {}, False, True];

test = {{"b", "c", "a", "e", "c"}, {"a", "e", "c", "d", "a"}, {"d",
"d", "e", "c", "d"}, {"a", "e", "d", "d", "d"}, {"e", "b", "d",
"d", "a"}};


Usage:

f[#, {"a", "b"}] & /@ test


{True, False, False, False, True}

f[#, {"b", "c", "e"}] & /@ test
`

{True, False, False, False, False}