File Exchange

image thumbnail


version (15.8 KB) by Erik
Find linear indices of duplicate elements in input.


Updated 20 Jan 2018

From GitHub

View Version History

View license on GitHub

findduplicates, find linear indices of duplicates in input
This function accepts the same input arguments as the unique function, which is the basis for this function.


[i1, i2] = findduplicates(A)
[i1, i2] = findduplicates(A, setOrder)
[i1, i2] = findduplicates(A, occurence)
[i1, i2] = findduplicates(A, _, 'rows')
[i1, i2] = findduplicates(A, 'rows', _)
[i1, i2, C, ia, ic] = findduplicates(_)

[i1, i2, C, ia, ic] = findduplicates(A, 'legacy')
[i1, i2, C, ia, ic] = findduplicates(A, 'rows', 'legacy')
[i1, i2, C, ia, ic] = findduplicates(A, occurrence, 'legacy')
[i1, i2, C, ia, ic] = findduplicates(A, 'rows', occurrence, 'legacy')
[i1, i2, C, ia, ic] = findduplicates(A, occurrence, 'rows', 'legacy')


[i1, i2] = findduplicates(A) finds the linear indices of duplicates in input A. The accepted input arguments are the same as accepted by the unique function. The first output, i1, contains the linear indices of the first duplicate elements into A that correspond to the duplicates indexed by i2, the second output. i1 may contain the same index multiple times if an element exists more than twice in A. If no duplicates exist, i1 and i2 are returned empty.

[i1, i2] = findduplicates(A, setOrder) supports the 'sorted' (default) or 'stable' flags of the unique function, but have no effect to find duplicates.

[i1, i2] = findduplicates(A, occurence) specifies which duplicate to consider as the first, occurence can be 'first' (default) or 'last'.

[i1, i2] = findduplicates(A, _, 'rows') and [i1, i2] = findduplicates(A, 'rows', _) treat each row of A as a single entity and returns indices of duplicate rows. You must specify A and optionally can specify setOrder or occurence. The 'rows' option does not support cell arrays.

[i1, i2, C, ia, ic] = findduplicates(_) also returns C, ia and ic such that [C, ia, ic] = unique(_). See the documentation for unique for their meaning and use.

The syntaxes with the 'legacy' option preserve the behaviour this function would have on R2012b and prior releases. The 'legacy' option must be the last input argument. It does not support categorical arrays, datetime arrays, duration arrays, tables or timetables.


Find the first indices of duplicates and the other duplicates' indices:

A = [9, 2, 9, 5]
[i1, i2] = findduplicates(A)


A =
9 2 9 5
i1 =
i2 =

This indicates that the first element in A has a duplicate at the third element.

Find the last indices of duplicate character arrays and the other duplicates' indices:

names = {'Charlie', 'Alice', 'Bob', 'Alice', 'Alice', 'Charlie'};
[i1, i2] = findduplicates(names, 'last')


i1 =

i2 =

This indicates that the character arrays at indices 6 and 5 have duplicates, 5 having two duplicates. The duplicate for index 6 is at index 1, the duplicates for index 5 are at indices 2 and 4.

More examples

Run findduplicatesdemo for more examples of findduplicates based on the examples of the unique function. See the contents of findduplicatesdemo.m for details.


NaN are considered as distinct values by the unique function, thus they are considered non-duplicates by findduplicates as well.



Cite As

Erik (2021). findduplicates (, GitHub. Retrieved .

Comments and Ratings (2)


@Stephen: thank you for commenting and suggesting to add examples. I've just released an update with examples and updated documentation! It may be a few hours before The MathWorks updates the downloadable zip, but you can always get the latest release here:

Stephen Cobeldick

@Erik: this sound interesting.... but it would be nice to have actual working examples shown in the description/help, just like MATLAB functions do. Then anyone browsing can see if it does what they need it to do without having to download it and run some demo script. Examples allow you to show off how great the your function is, so don't skimp on them!

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!