TransWikia.com

How can I list the common elements of two lists?

Mathematica Asked on July 18, 2021

The elements of both lists are given as

A={9,5,9,3}, B={9,9,5,5}

In this case, how can I list the list {9,5,9} of the common elements of the two lists?

(The order does not matter.)

One Answer

If you need the duplicate 9 in the output:

 In[1]:= Flatten[KeyValueMap[ConstantArray, Merge[KeyIntersection[Counts /@ {{9, 5, 9, 3}, {9, 9, 5, 5}}], Min]], 1]
Out[1]:= {9, 9, 5}

If you didn't need the duplicate 9 you could just use the Intersection function.

Correct answer by qbg on July 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP