Home » Python » Python programs

# Find the union and intersection of two arrays in Python

Here, we are going to learn **how to find the union and intersection of two arrays in Python programming language?**

Submitted by Bipin Kumar, on October 25, 2019

Two arrays will be given by the user and we have to **find the union and intersection of these arrays in the Python programming**. To **find the union and intersection of these arrays**, we will use the **bitwise or (|)** and **bitwise and (&)** respectively between the set of the given arrays. Before going to solve this problem we will learn about the union and intersection.

### Union and intersection of two arrays

A list that has the common distinct element from both arrays and if there are repetitions of the element then only one occurrence is considered, known as the union of both arrays.

A list that has common distinct elements from both arrays, is the intersection of both arrays.

**Algorithm to solve this problem**

- Initially, we will take two lists from the user which may have repeated numbers or not.
- We will take the
**bitwise or (|)**between the sets of both arrays to find union and assign it into a variable A in the form of lists. - To find the intersection of both arrays, we will use the
**bitwise and (&)**between the sets of given arrays and assign it into a variable B in the form of lists. - Print variable A and B which is our required output.

Let's start writing the Python program by the implementation of the above algorithm.

**Code:**

a=list(map(int,input('Enter elements of first list:').split())) b=list(map(int,input('Enter elements of second list:').split())) A=list(set(a)|set(b)) B=list(set(a)&set(b)) print('Union of the arrays:',A) print('intersection of the arrays:',B)

**Output**

Enter elements of first list: 3 4 6 4 4 6 7 41 Enter elements of second list: 78 3 5 7 -1 9 2 -5 Union of the arrays: [2, 3, 4, 5, 6, 7, 41, 9, 78, -5, -1] intersection of the arrays: [3, 7]

**set() function** is inbuilt in Python which is used to convert a list into another list which does not contain duplicate or repeated elements.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions