# Pattern for k distinct elements of a set of n elements

I would like a pattern which takes as an argument a set with $n$ elements, and an integer $k$ which is less than $n$ and greater than 1, and which matches against any $k$ distinct elements of the set, in a nice elegant way.

So I'd like ChoosePattern[set,k] which satisfies, for example,

MatchQ[{a,b}, ChoosePattern[{a,b,c},2]]
MatchQ[{c,b}, ChoosePattern[{a,b,c},2]]
MatchQ[{1,2,4,3}, ChoosePattern[Range,4]]
MatchQ[{1,8,5,6}, ChoosePattern[Range,4]]


And doesn't match

MatchQ[{a,d}, ChoosePattern[{a,b,c},2]]
MatchQ[{c,b,a}, ChoosePattern[{a,b,c},2]]
MatchQ[{1,2,2,3}, ChoosePattern[Range,4]]
MatchQ[{1,8,5,6,7}, ChoosePattern[Range,4]]


Thank you

EDIT: Just a small addition to kglr's very nice solution, here are my pattern and matching functions for choosing either any number, or a fixed number, or a range of possible numbers, of distinct elements from a given set;

ChoosePattern[set_List,
k_Integer] := _List?(CountDistinct[#] === k && SubsetQ[set, #] &)
ChoosePattern[
set_List, {k1_Integer,
k2_Integer}] := _List?(CountDistinct[#] === Length[#] &&
k1 <= Length[#] <= k2 && SubsetQ[set, #] &)
ChoosePattern[
set_List] := _List?(CountDistinct[#] === Length[#] &&
SubsetQ[set, #] &)
ChooseQ[set_List, subset_List, k_Integer] :=
MatchQ[subset, ChoosePattern[set, k]]
ChooseQ[set_List, subset_List, {k1_Integer, k2_Integer}] :=
MatchQ[subset, ChoosePattern[set, {k1, k2}]]
ChooseQ[set_List, subset_List] := MatchQ[subset, ChoosePattern[set]]


Also

ClearAll[cKp]
cKp[e_,n_]:= _List?(CountDistinct[#] == n && SubsetQ[e, #]&)

• Very elegant thank you – Joe May 9 '18 at 8:50
• @Joe, thank you for the accept. – kglr May 9 '18 at 17:35

This seems to pass all those tests:

ChooseKPattern[elems_List, n_Integer] := Block[{list}
, Condition @@ Hold[
list_List
, Length[DeleteDuplicates @ list] == n && ContainsAll[elems, list]
]
]

• this works too...Hold[list_List, Length@Intersection[elems, list] == n] (I don't have ContainsAll ..) – george2079 May 8 '18 at 16:42
• Thank you this is nice and it works – Joe May 9 '18 at 8:49