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.
Szabolcs
source share