OneBite.Dev - Coding blog in a bite size

find the common elements in two arrays in python

Code snippet on how to find the common elements in two arrays in python

def common(a, b):
    return list(set(a).intersection(set(b)))

This code creates a new function called common that takes two parameters a and b, which are both arrays. The function then returns the list of elements that are common to both a and b, by first converting the arrays into sets and then using the intersection() set function to find the common elements. The elements found are then returned as a list. By using sets, the time complexity of the code is O(n), making it a very efficient solution.

python