Find the Union and Intersection of the two sorted arrays in Python

Get Prepinsta Prime

Get all 200+ courses offered by Prepinsta

Never Miss an OffCampus Update

Get OffCampus Updates on Social Media from PrepInsta

Follow us on our Media Handles, we post out OffCampus drives on our Instagram, Telegram, Discord, Whatsdapp etc.

Get Hiring Updates
Amazon,Google,Delottie & 30+companies are hiring ! Get hiring Updates right in your inbox from PrepInsta

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others.

Get PrepInsta Prime Subscription

Get access to all the courses that PrepInsta offers, check the out below -

Companies

TCS, Cognizant, Delloite, Infosys, Wipro, CoCubes, KPMG, Amazone, ZS Associates, Accenture, Congnizant & other 50+ companies

Programming

Data Structures, Top 500 Codes, C, C++, Java Python & other 10+ subjects

Skills

Full Stack Web Development, Data Science, Machine Learning, AWS Cloud, & other 10+ skills and 20+ projects

OffCampus Updates

Never Miss OffCampus Updates

Get OffCampus Update from PrepInsta

Follow us on our Social Media Handles

PYTHON PROGRAM TO UNION AND INTERSECTION OF TWO SORTED LIST

Union and Intersection of sorted array

In this article we will see a python program to find Union and Intersection of sorted array given by the user as input. User will give two list or array as input. We will then perform intersection and union on those list and then we will return the output.

  • Union: List of all the elements present in both the list.
  • Intersection: List of all the elements present in both the list.

Method 1

  • In this method we will use inbuilt function for union and intersection.
  • For union we will use | operator. Eg. list1| list2
  • For intersection we will use & operator. Eg. list1 & list2

Algorithm

  • Step 1: Declare two list, list1 and list2. 
  • Step 2: Call function union
  • Step 3: Call function intersection

Algorithm for function union

  • Step 1: Declare list1 and list 2 as set as inbuilt function works on set not on list.
  • Step 2: Print list1 and list2.
  • Step 3: Using inbuilt function, print the union of two list.

Algorithm for function intersection

  • Step 1: Declare list1 and list 2 as set as inbuilt function works on set not on list.
  • Step 2: Print list1 and list2.
  • Step 3: Using inbuilt function, print the intersection of two list.
UNION AND INTERSECTION OF TWO SORTED LIST IN PYTHON

Python Code

Run
def union(list1, list2):
    # Make list1 and list2 as set
    list1 = set(list1)
    list2 = set(list2)

    # Print list1, list2
    print("List 1= ", list1)
    print("List 2= ", list2)

    # Using inbuilt function union print the result
    print("Union of two list: ", list1 | list2)


def intersection(list1, list2):
    # Make list1 and list2 as set
    list1 = set(list1)
    list2 = set(list2)

    # Using inbuilt function intersection print the result
    print("Intersection of two list: ", list1 & list2)


# Declare two empty list list1 and list2
list1 = [1, 2, 3, 4]
list2 = [4, 5, 6, 7]

# call the function union
union(list1, list2)

# call the function intersection
intersection(list1, list2)

Output

List 1= {1, 2, 3, 4}
List 2= {4, 5, 6, 7}
Union of two list: {1, 2, 3, 4, 5, 6, 7}
Intersection of two list: {4}

Method 2

Algorithm

  • Step 1: Declare two empty list, list1 and list2.
  • Step 2: Call function union
  • Step 3: Call function intersection

Algorithm for function union

  • Step 1: Make new_arr as new list
  • Step 2: Iterate on the elements of list1. If the element is not in new_arr then, append that element in new_arr.
  • Step 3: Iterate on the elements of list2. If the element is not in new_arr then, append that elementd in new_arr.
  • Step 4: Sort the new_arr.
  • Step 5: Print new_arr

Algorithm for function intersection

  • Step 1: Make new_arr as new list
  • Step 2: Iterate on the elements of list1. If the element is not in new_arr  and also the elemnent is present in list2, then, append that element in new_arr. 
  • Step 3: Sort the new_arr.
  • Step 4: Print new_arr

Python Code

Run
def union(list1, list2):
    new_arr = []
    for i in list1:
        if i not in new_arr:
            new_arr.append(i)
    for i in list2:
        if i not in new_arr:
            new_arr.append(i)
    new_arr.sort()
    print("Union of above two array: ", new_arr)


def intersection(list1, list2):
    new_arr = []
    for i in list1:
        if (i not in new_arr) and (i in list2):
            new_arr.append(i)
    new_arr.sort()
    print("Intersection of above two array: ", new_arr)


# Declare two empty list list1 and list2
list1 = [1, 2, 3, 4]
list2 = [4, 5, 6, 7]

# original lists
print("list 1=", list1)
print("list 2=", list2)

# call the function union
union(list1, list2)

# call the function intersection
intersection(list1, list2)

Output

list 1= [1, 2, 3, 4]
list 2= [4, 5, 6, 7]
Union of above two array: [1, 2, 3, 4, 5, 6, 7]
Intersection of above two array: [4]

Comments

One comment on “Find the Union and Intersection of the two sorted arrays in Python”


  • Prayansh

    a=[1,2,3,4,5]
    b=[4,5,6,7,8]
    intersection1=[]
    union1=a[:]
    for i in b:
    if i not in a:
    union1.append(i)
    if i in a:
    intersection1.append(i)

    print(intersection1)
    print(union1)