DeleteAdjacentDuplicates[list]
deletes all duplicates in runs of identical elements in list.
DeleteAdjacentDuplicates[list,test]
applies test to pairs of consecutive elements to determine whether they should be considered duplicates.
DeleteAdjacentDuplicates
DeleteAdjacentDuplicates[list]
deletes all duplicates in runs of identical elements in list.
DeleteAdjacentDuplicates[list,test]
applies test to pairs of consecutive elements to determine whether they should be considered duplicates.
Details
- DeleteAdjacentDuplicates[list] deletes all but the first occurrence of each element that appears in a run of identical elements in list.
- DeleteAdjacentDuplicates also works on associations.
- DeleteAdjacentDuplicates never reorders elements, but only removes them.
- DeleteAdjacentDuplicates[list] is equivalent to DeleteAdjacentDuplicates[list,SameQ].
Examples
open all close allBasic Examples (3)
Delete adjacent duplicated elements:
DeleteAdjacentDuplicates[{3, 2, 2, 2, 3, 3, 1}]Delete adjacent elements that coincide under Equal:
DeleteAdjacentDuplicates[{3, 2., 2, 2`20, 3 / 2, 1.5, 1}, Equal]Delete elements whose values duplicate those just before them in the association:
DeleteAdjacentDuplicates[<|a -> 1, b -> 1, c -> 2, d -> 2, e -> 1|>]Scope (2)
Delete elements unless they are larger than the preceding ones:
DeleteAdjacentDuplicates[{1, 3, 5, 4, 3, 4, 1, 9, 9, 7, 8}, Greater]In DeleteAdjacentDuplicates[list], list may be any nonatomic expression:
DeleteAdjacentDuplicates[f[x, x, y, y, y, x, x, y, z]]Properties & Relations (2)
DeleteAdjacentDuplicates is similar to taking the first of each item from Split:
list = RandomInteger[2, 20]Split[list][[All, 1]]DeleteAdjacentDuplicates[list]Delete sublists that have the same total:
list = {{1, 2}, {0, 3}, {1, 1}, {2, 0}, {2, 1}}DeleteAdjacentDuplicates[list, Total[#1] == Total[#2]&]The equivalent operation can be achieved using SplitBy as well:
SplitBy[list, Total][[All, 1]]Related Guides
History
Text
Wolfram Research (2022), DeleteAdjacentDuplicates, Wolfram Language function, https://reference.wolfram.com/language/ref/DeleteAdjacentDuplicates.html.
CMS
Wolfram Language. 2022. "DeleteAdjacentDuplicates." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/DeleteAdjacentDuplicates.html.
APA
Wolfram Language. (2022). DeleteAdjacentDuplicates. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/DeleteAdjacentDuplicates.html
BibTeX
@misc{reference.wolfram_2026_deleteadjacentduplicates, author="Wolfram Research", title="{DeleteAdjacentDuplicates}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/DeleteAdjacentDuplicates.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_deleteadjacentduplicates, organization={Wolfram Research}, title={DeleteAdjacentDuplicates}, year={2022}, url={https://reference.wolfram.com/language/ref/DeleteAdjacentDuplicates.html}, note=[Accessed: 13-June-2026]}