Skip to content

DeleteDuplicates

Status: Stable

documented, exercised by the test suite and/or worked examples, with no known limitations recorded.

Description

DeleteDuplicates[list]
    returns list with duplicate elements removed, keeping the first
    occurrence of each element and preserving the original order.
DeleteDuplicates[list, test]
    treats two elements as duplicates when test[a, b] yields True.

Examples

All examples below are verified against the current Mathilda build.

In[1]:= DeleteDuplicates[{a, a, b, a, c, b, a}]
Out[1]= {a, b, c}

Implementation notes

Algorithm. builtin_deleteduplicates (in src/list.c) keeps the first occurrence of each distinct element. The default (no test) path builds a HashTable keyed by expression hash/equality and inserts each element only if ht_find reports it absent, giving expected O(N) behavior. When a custom equivalence test is supplied as the second argument it falls back to an O(N²) scan, evaluating test[elem, kept_j] and treating a True result as a duplicate.

Data structures. The HashTable from src/list.c's hashing utilities (open-addressing over Expr* via expr_hash/expr_eq); a pre-sized Expr** collects the survivors before the result List (the original head) is built.

  • Protected.
  • Preserves the order of first occurrences.

Attributes: Protected.

Implementation status

Stable — documented, exercised by the test suite and/or worked examples, with no known limitations recorded.

References

Notes & additional examples

Worked examples

In[1]:= DeleteDuplicates[{1,2,1,3,2,4}]
Out[1]= {1, 2, 3, 4}
In[1]:= DeleteDuplicates[{a,b,a,c,b,d,a}]
Out[1]= {a, b, c, d}
In[1]:= DeleteDuplicates[{1,2,3,4,5,6}, Mod[#1,3]==Mod[#2,3]&]
Out[1]= {1, 2, 3}
In[1]:= DeleteDuplicates[{1,-1,2,-2,3,-3}, Abs[#1]==Abs[#2]&]
Out[1]= {1, 2, 3}

Notes

DeleteDuplicates[list] keeps the first occurrence of each distinct element and preserves the original order. With a two-argument equivalence test DeleteDuplicates[list, test] two elements are treated as duplicates when test[a, b] is True, so you can deduplicate by an arbitrary relation rather than literal equality — picking one representative per residue class modulo 3, or one per absolute value, in the examples above.