Skip to main content
1 of 2
martin
  • 8.9k
  • 4
  • 27
  • 71

Delete duplicates from hierarchy of lists

Is it possible for Mathematica to compare a number of lists of different sizes, and delete duplicates in a hierarchical manner?

For example, given the following lists:

list 1 = {2, 5, 10, 11, 12, 17, 18};
list 2 = {5, 8, 11, 16, 19, 21};
list 3 = {5, 8, 9, 11, 16, 19, 21, 23};

I would like to output:

list 1 = {2, 5, 10, 11, 12, 17, 18}
list 2 = {8, 16, 19, 21}
list 3 = {9, 23}

where list 1 keeps all its original elements, list 2 keeps only elements belonging to list 2 and not list 1, and list 3 keeps only those elements not contained in lists 1 & 2.

If this will take an inordinately long time, I am happy with just comparing the list with the previous list and deleting the duplicates from the second list.

martin
  • 8.9k
  • 4
  • 27
  • 71