Version 10 offers Counts
which is useful for this question:
$list = {{1, 2, 3}, {4, 5, 6, 7}, {1}, {1, 8}, {9, 10}, {11, 6, 12, 13, 14, 15, 16}};
count = Counts @ Flatten @ $list
(* <| 1->3, 2->1, 3->1, 4->1, 5->1, 6->2, 7->1, 8->1, 9->1, 10->1, 11->1,
12->1, 13->1, 14->1, 15->1, 16->1 |> *)
count[6]
(* 2 *)
(A shame that Counts
does not accept a level specification.)
With this information, we can use DeleteCases
to express the requirement fairly directly:
DeleteCases[$list, x_ /; count[x] >= 2, {2}]
(* {{2, 3}, {4, 5, 7}, {}, {8}, {9, 10}, {11, 12, 13, 14, 15, 16}} *)
Or we could use Pick
to keep the desired values:
Pick[$list, Map[count[#] < 2 &, $list, {2}]]
(* {{2, 3}, {4, 5, 7}, {}, {8}, {9, 10}, {11, 12, 13, 14, 15, 16}} *)
With version 10.2, we can replace all rejected values with Nothing
:
$list /. x_Integer /; count[x] >= 2 -> Nothing
(* {{2, 3}, {4, 5, 7}, {}, {8}, {9, 10}, {11, 12, 13, 14, 15, 16}} *)
... although in earlier versions the cryptic (##&[])
can express the idea of Nothing
.
Each of these techniques leaves an empty sublist when all of its original elements are rejected. We can use the operator DeleteCases[{}]
to get rid of such empty remnants. For example:
DeleteCases[$list, x_ /; count[x] >= 2, {2}] // DeleteCases[{}]
(* {{2, 3}, {4, 5, 7}, {8}, {9, 10}, {11, 12, 13, 14, 15, 16}} *)