Discussion:
How to find duplicate in a list?
(too old to reply)
Dr. M Kanagasabapathy
2017-07-10 02:17:31 UTC
Permalink
Raw Message
Dear Maxima members,
Is there any specific function to find the duplicate entries in the list.
I know the function Unique(L) returns all members in the list but not
duplicate.
It excludes the duplicate.

L: [a,b,a,c,d,f,c]
In this list
Duplicate(L) should returns a, c.


Thanks for the help.
Regards
Mario Rodriguez
2017-07-10 08:42:24 UTC
Permalink
Raw Message
Post by Dr. M Kanagasabapathy
Dear Maxima members,
Is there any specific function to find the duplicate entries in the list.
I know the function Unique(L) returns all members in the list but not
duplicate.
It excludes the duplicate.
L: [a,b,a,c,d,f,c]
In this list
Duplicate(L) should returns a, c.
Thanks for the help.
Regards
I think we don't have such a function, but we can build one with some
help of package descriptive:

load(descriptive)$
nfreq(l, k):= submatrix(subsample (transpose(apply(matrix,discrete_freq
(l))), lambda([v], v[2] = k)),2);

nfreq returns a column matrix with the elements in list l which are
repeated k times. Examples:

m: [1, 0, 1, 1, 2, 6, 2, 5, 0, 4];
nfreq(m,2); /* duplicated entries */
nfreq(m,3); /* triplicated entries */
--
Mario

http://www.tecnostats.net
Richard Fateman
2017-07-10 13:59:00 UTC
Permalink
Raw Message
L:[a,b,c,a,b,d];

f[x]:=0;

for i in L do f[i]:f[i]+1;

Look at rest(arrayinfo(f),2) for the list of (unique) elements.
To see which are duplicates, look at their values.

e.g. f(a) will be 2.
Richard Fateman
2017-07-10 14:59:59 UTC
Permalink
Raw Message
maybe I should complete the program

L:[a,b,c,a,b,d]$
f[x]:=0$
for i in L do f[i]:f[i]+1$
dups:[]$
for i in rest(arrayinfo(f),2) do if f[first(i)]>1 then push (first(i),dups)$
dups;

returns [b,a]
Stavros Macrakis (Σταῦρος Μακράκης)
2017-07-10 15:38:43 UTC
Permalink
Raw Message
Here's a simple approach:

dups(l) :=
block([res : []],
while l # []
do (if member(first(l), rest(l)) then push(first(l), res),
l : rest(l)),
setify(res))$

This of course takes O(len(l)^2), but that should be fine for most
applications.

Fateman's code is O(len(l)), which is nice for larger applications. When
packaging it as a function, be careful to include a "local" declaration for
the local array:

dups(l) :=
block([dups:[],counts], local(counts),
counts[x]:=0,
for i in l do counts[i]: counts[i]+1,
for i in rest(arrayinfo(counts),2) do
if counts[first(i)]>1 then push(first(i),dups),
dups)$
Post by Richard Fateman
maybe I should complete the program
L:[a,b,c,a,b,d]$
f[x]:=0$
for i in L do f[i]:f[i]+1$
dups:[]$
for i in rest(arrayinfo(f),2) do if f[first(i)]>1 then push
(first(i),dups)$
dups;
returns [b,a]
------------------------------------------------------------
------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Maxima-discuss mailing list
https://lists.sourceforge.net/lists/listinfo/maxima-discuss
Loading...