3

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.

yode
  • 26,686
  • 4
  • 62
  • 167
  • Related converse problem: http://mathematica.stackexchange.com/q/8154/121 – Mr.Wizard Dec 10 '16 at 16:51
  • 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. – Mr.Wizard Dec 10 '16 at 16:52
  • @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. :) – yode Dec 10 '16 at 17:35
  • Please consider editing this question to highlight the difference from the existing linked question. That will automatically add this to the reopen review queue. – Mr.Wizard Dec 11 '16 at 05:30

2 Answers2

4
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}}  *)
Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
2

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}}

kglr
  • 394,356
  • 18
  • 477
  • 896
  • 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. :) – yode Dec 10 '16 at 17:38
  • How long have you know the package GraphComputation introduced? – yode Dec 10 '16 at 18:02