3
$\begingroup$

This question already has an answer here:

I have a such list:

data={{4, 2}, {4, 2, 3}, {4, 5}, {4, 2, 3, 5}, {4, 6}}

Because the {4,2},{4,2,3},{4,5} is subset of {4, 2, 3, 5},so I want to delete they to get {{4,2,3,5},{4,6}}.There is mess method:

DeleteCases[data, _?(Or @@ 
     Function[list, SubsetQ[list, #]] /@ DeleteCases[data, #] &)]

{{4,2,3,5},{4,6}}

But I think must have some concise method can do this,such as by DeleteDuplicates,Union and other something.But I have fail to implement that.

$\endgroup$

marked as duplicate by Mr.Wizard list-manipulation Dec 10 '16 at 16:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Related converse problem: mathematica.stackexchange.com/q/8154/121 $\endgroup$ – Mr.Wizard Dec 10 '16 at 16:51
  • $\begingroup$ I have marked this question as a duplicate. Please review the "original" linked at the top of your post and let me know if you disagree. $\endgroup$ – Mr.Wizard Dec 10 '16 at 16:52
  • $\begingroup$ @Mr.Wizard Well,I think this two posts have little difference still,maybe they are a good reference for each other.But I will would let the reader to decide they are duplicate or not. :) $\endgroup$ – yode Dec 10 '16 at 17:35
  • $\begingroup$ Please consider editing this question to highlight the difference from the existing linked question. That will automatically add this to the reopen review queue. $\endgroup$ – Mr.Wizard Dec 11 '16 at 5:30
4
$\begingroup$
data = {{4, 2}, {4, 2, 3}, {4, 5}, {4, 2, 3, 5}, {4, 6}};

DeleteDuplicates[SortBy[data, Length] // Reverse, 
 ContainsAll[#1, #2] &]

(*  {{4, 2, 3, 5}, {4, 6}}  *)

Or as yode pointed out, this can be simplified to

DeleteDuplicates[SortBy[data, Length] // Reverse, ContainsAll]

(*  {{4, 2, 3, 5}, {4, 6}}  *)
$\endgroup$
  • $\begingroup$ +1)..It's seem we can omit the [#1, #2] & :) $\endgroup$ – yode Dec 9 '16 at 17:02
  • 1
    $\begingroup$ Slightly simpler: DeleteDuplicates[Reverse@Sort@data, ContainsAll] (+1) $\endgroup$ – kglr Dec 10 '16 at 1:59
2
$\begingroup$

Also

Pick[VertexList@#, VertexInDegree@#, 1] &@ RelationGraph[SubsetQ, data]

{{4, 2, 3, 5}, {4, 6}}

Or

GraphComputation`SourceVertexList @RelationGraph[And[UnsameQ@##,SubsetQ@##]&, data]

{{4, 2, 3, 5}, {4, 6}}

$\endgroup$
  • $\begingroup$ Upvote for And[UnsameQ@##, SubsetQ@##] &.I have to say I consider it for a long time to avoid produce multigraph but fail to do it. :) $\endgroup$ – yode Dec 10 '16 at 17:38
  • $\begingroup$ How long have you know the package GraphComputation introduced? $\endgroup$ – yode Dec 10 '16 at 18:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.