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

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