Function Repository Resource:

DuplicatesBy

Source Notebook

Select all the elements from a list for which a given function returns the same answer twice or more

Contributed by: Sander Huisman

ResourceFunction["DuplicatesBy"][list,f]

gives back the elements e for which f[e] appears twice or more in list.

Details and Options

Results are given back in the order of first appearance.

Examples

Basic Examples (2) 

Select all duplicates by their absolute value:

In[1]:=
ResourceFunction["DuplicatesBy"][{-1, 1, 2, 3, 5, -2, 6}, Abs]
Out[1]=

Select all duplicates by their first element:

In[2]:=
ResourceFunction[
 "DuplicatesBy"][{{1, a}, {1, b}, {5, a}, {2, c}, {3, d}, {4, b}, {5, e}, {6, f}}, First]
Out[2]=

Properties and Relations (1) 

DuplicatesBy is related to the resource function Duplicates:

In[3]:=
list = {1, 1, 2, 3, 5, 2, 6};
ResourceFunction["DuplicatesBy"][list, Identity] == ResourceFunction["Duplicates"][list]
Out[4]=

Possible Issues (1) 

An empty list is returned if there are no duplicates:

In[5]:=
ResourceFunction["DuplicatesBy"][{-1, 1, 2, 3, 5, -2, 6}, Sqrt]
Out[5]=

Publisher

SHuisman

Version History

  • 1.0.0 – 11 March 2020

Related Resources

License Information