DeleteDuplicates while maintaining subscription structure

I want DeleteDuplicates from a list of lists to maintain a consistent substring structure.

eg.

{{1, 7, 8}, {4, 3}, {4, 1, 9}, {9, 2}}

becomes

{{1, 7, 8}, {4, 3}, {9}, {2}}

This is the size of the nesting, and empty signatures are in order.

+7
source share
3 answers

This is a classic trick:

 list = {{1, 7, 8}, {4, 3}, {4, 1, 9}, {9, 2}} Module[{f}, f[x_] := (f[x] = Sequence[]; x); Map[f, list, {2}] ] 

To understand how this works, consider what happens when f[1] is evaluated for the first time. f[1] will be evaluated to (f[1]=Sequence[]); 1 (f[1]=Sequence[]); 1 , and then just 1 . So, now definitions have been made for f[1] . The next time f[1] is evaluated, it will simply evaluate the value of Sequence[] .

So, the first time f is evaluated for the argument, it returns that argument. The second (or third, etc.) time is estimated, it returns Sequence[] . Sequence[] has the property that it is completely removed from the expressions, i.e. {1, Sequence[], 3} will be evaluated to {1, 3} .

To summarize, what the function does: when you first look at an element, it replaces the element itself (leaves it alone). The second time he sees the same element, he removes it. I displayed this function at “ level 2 ”, so it will only affect sublist items.

+11
source

You can use Complement for this, and the only thing you need to do is save all the elements that you have already seen. At the beginning, the list of all noticed elements of a empty, and then you add in stages all the numbers that are in the sublists. This cumulative list is used at each step to delete all numbers that have already been viewed:

 DeleteDuplicatesList[l_List] := Block[{a = {}}, Table[With[{b = a}, a = Union[a, c]; Complement[c, b]], {c, l}] ]; l = {{1, 7, 8}, {4, 3}, {4, 1, 9}, {9, 2}}; DeleteDuplicatesList[l] 
+3
source

It is not elegant, but it does its job:

 t = {{1, 7, 8}, {4, 3}, {4, 1, 9}, {9, 2}}; Delete[t, Flatten[Rest[Position[t, #]] & /@ (DeleteDuplicates[Flatten[t]]), 1]] 

DeleteDuplicates ... will return a set, not multi-level, of numbers from t. Rest[Position[ ... will return the duplicate position. Delete deletes the specified duplicates.

+2
source

All Articles