# Selecting elements of a list based on frequency

Suppose I have a list as follow:

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


Now I am going to flatten it and apply Counts to count each element:

l // Flatten // Counts

<|"a" -> 4, "b" -> 3, "c" -> 2, "d" -> 1, "e" -> 1|>


Now I want to do followings:

1. From the list how can I retain only sublists whose components have frequency more than 1 in the overall list l, namely the output should look like this:

{{"a", "b", "c"}, {"a", "b"}}


as all "a", "b" and "c" have frequency above 1.

1. how can I delete those sublists that contain any components that have frequency 1 from l, namely the output should look like:

{{"a", "b", "c"}, {"a", "b"}, {"a", "b"}, {"a", "c"}}


Here's an example:

counts = l // Flatten // Counts;
mask = Map[counts[#] != 1 &, l, {2}];


{{"a", "b", "c"}, {"a", "b"}, {"a", "b"}, {"a", "c"}}

And for the other one,

mask = Map[counts[#] > 1 &, l, {2}];


{{"a", "b", "c"}, {"a", "b"}}

Another way:

Map[
If[counts[#] != 1, #, Nothing] &,
l, {2}]


{{"a", "b", "c"}, {"a", "b"}, {"a", "b"}, {"a", "c"}}

If[And @@ (counts[#] > 1 & /@ #), #, Nothing] & /@ l


{{"a", "b", "c"}, {"a", "b"}}

Alternatives using Select rather than Pick:

l = {{"a", "b", "c"}, {"a", "b"}, {"a", "d", "b"}, {"a", "c", "e"}};
counts = Counts[Flatten@l];

1. To obtain the sublists whose elements are repeated in the list:

Select[ContainsNone[Keys@Select[# == 1 &]@counts]@l
(* Out: {{"a", "b", "c"}, {"a", "b"}} *)

2. To remove those sublists that contain non-repeated elements in the overall list:

DeleteCases[Alternatives @@ Keys@Select[# == 1 &]@counts] /@ l
(* Out: {{"a", "b", "c"}, {"a", "b"}, {"a", "b"}, {"a", "c"}} *)