embert
embert

Reputation: 7592

Delete duplicates in array but keep first occurrence

The following code removes duplicate values

numarr = [1 2 2 3 3 3 4 4 4 4];
%// Filter doubled values
[n_, bin]       = histc(numarr, unique(numarr));
multiple_       = find(n_ > 1);
mult_indices_   = ismember(bin, multiple_);

numarr(mult_indices_) = [];

%// output     
numarr =  1

How to adapt it, that the first occurence of any duplicate remains?

i.e. that the output will be

numarr =

     1     2     3     4

Upvotes: 1

Views: 207

Answers (1)

Robert Seifert
Robert Seifert

Reputation: 25232

Use unique with the 'stable' property:

a = [1 3 2 5 2 7 1 3 4 5 6 8 2];
output = unique(a,'stable')

which will keep the order and therefore keeps the first occurrance of every value, as desired.

output =

     1     3     2     5     7     4     6     8

Without 'stable' the output gets sorted.


Regarding your comment: To get the indices of the removed duplicates you need the second output of unique and setdiff:

[output, kept_idx] = unique(a,'stable')
removed_idx = setdiff(1:numel(a),kept_idx)

removed_idx =

     5     7     8    10    13

Upvotes: 2

Related Questions