Javatpoint Logo
Javatpoint Logo

Union and Intersection Of Two Sorted Arrays In Java

The union and intersection of two sorted arrays are fundamental operations in computer science and data analysis. In Java, these operations can be performed efficiently on two sorted arrays by utilizing their inherent order. The union of two arrays is the set of all elements that are in either array, while the intersection is the set of all elements that are common to both arrays. These operations are commonly used in a wide range of applications, including database management, information retrieval, and statistics.

Union

In Java, the union of two sorted arrays can be obtained by merging the two arrays and removing duplicates. It is a common operation in programming and is often used in tasks such as merging two lists or finding the common elements between two lists. The union operation combines the elements of both arrays into a new array that contains all unique elements from both arrays.

Given two sorted arrays, find their union and Intersection.

Example 1:

Input:

arr1[] = {2, 5, 6}

arr2[] = {4, 6, 8, 10}

Output:

Union: {2, 4, 5, 6, 8, 10}

Intersection : {6}

Example 2:

Input:

arr1[] = {2, 3, 6, 7, 9}

arr2[] = {1, 3, 5, 6, 8}

Output:

Union: {1, 2, 3, 5, 6, 7, 8, 9}

Intersection : {3, 6}

Example 3:

Input:

arr1[] = {4, 6, 8, 10}

arr2[] = {1, 2, 4, 5, 8}

Output:

Union: {1, 2, 4, 5, 6, 8, 10}

Intersection : {4, 8}

Approach: Union of arrays arr1[] and arr2[]

The union of two arrays, arr1[] and arr2[], is a new array that contains all distinct elements from both arrays. To obtain the union of two sorted arrays, we can use a two-pointer approach to traverse the arrays and add elements to the union array.

Algorithm

  1. Create an empty ArrayList to store the result.
  2. Initialize two pointers, i and j, to the start of arr1 and arr2, respectively.
  3. Iterate until pointers i and j are less than their respective array lengths.
  4. If arr1[i] is less than arr2[j], add arr1[i] to the result ArrayList and Increment i.
  5. If arr1[i] is greater than arr2[j], add arr2[j] to the result ArrayList and increment j.
  6. If arr1[i] is equal to arr2[j], add arr1[i] to the result ArrayList and Increment both i and j.
  7. If there are remaining elements in arr1 or arr2, add them to the result ArrayList.
  8. Convert the result ArrayList to an array of integers and return i

Implementation

Filename: ArrayUnion.java

Output:

Union of two arrays: 1 2 3 4 5 6 7 8

Complexity Analysis:

The time complexity of the code is O(m+n), where m and n are the lengths of the two input arrays. It is because the while loop that iterates through both arrays has a maximum of m+n iterations, as each iteration increments either the i or j pointer until one of the arrays is fully traversed.

The space complexity is also O(m+n), as the only additional space used is for the variables i, j, m, and n and the printed Output.

Approach: Using Set

The approach using a Set is a simple and efficient way to find the union of two sorted arrays. By inserting all the elements from both arrays into a Set, duplicates are automatically removed, and the resulting Set contains all the unique elements from both arrays.

Filename: UnionOfSortedArrays.java

Output:

1 2 3 4 5 6 7 8 9 10

Complexity Analysis: The time complexity of the given code is O(mlog(m) + nlog(n)), where m and n are the lengths of the input arrays arr1 and arr2. The time complexity is improved by adding all the elements of both arrays to a TreeSet, which is implemented using a self-balancing binary search tree. The add() operation on a TreeSet has a time complexity of O(log(n)), where n is the size of the Set.

The space complexity of the code is O(m+n), as we are using a TreeSet to store the unique elements of both arrays, and the size of the Set can be at most m+n.

Approach: Using HashMap

The union of two sorted arrays can be implemented in Java using a HashMap to efficiently store and retrieve the elements of the arrays while removing duplicates. The approach involves iterating through the arrays using two pointers, adding the elements to the HashMap, and returning the keys of the HashMap as the union of the two arrays.

Filename: UnionOfSortedArrays.java

Output:

[1, 2, 3, 4, 5, 6, 7]

Complexity Analysis: The algorithm's time complexity is O(m+n), where m and n are the lengths of the two input arrays. The time complexity of adding an element to a HashMap and retrieving its keys is constant time, O(1), which contributes to the overall efficiency of the algorithm as it loops through both arrays only once

The space complexity of the algorithm is O(m+n) as well since the size of the HashMap and the ArrayList will be, at most, the size of the two input arrays combined.Intersection of arrays arr1[] and arr2[]

Intersection

Intersection is a common set operation in computer science that involves finding the common elements between two sets. In Java, we can find the intersection of two arrays by iterating through each element of one array and checking if it exists in the other array. The resulting array will contain the elements that are present in both arrays.

Approach: Intersection of arrays arr1[] and arr2[]

Algorithm

To find the Intersection of two arrays, arr1, and arr2, we can follow these steps:

  1. Initialize two pointers, i and j, at the start of arr1 and arr2, respectively.
  2. Iterate over both arrays using the two-pointers.
    • If arr1[i] is equal to arr2[j], add the element to the intersection list, and increment both pointers i and j.
    • Otherwise, if arr1[i] is less than arr2[j], Increment i.
    • Otherwise, if arr1[i] is greater than arr2[j], increment j.
  3. Return the intersection list.

Implementation

Filename: IntersectionOfArrays.java

Output:

[3, 4, 5]

Complexity Analysis: The time complexity of the program is O(m + n), and space complexity of program is O(min(m, n).

Approach: Using Tree Set

The main goal of this approach is to create a data structure using a tree set that can hold all the distinct elements found in arri[]. Then compare the elements arr2[] with the tree set and check if that is considered in the intersection to avoid duplicates.

Filename: TreeSetDemo.java

Output:

2 3 4 5

Complexity Analysis: The time complexity of the findIntersection Method is O(nlogn) because it involves adding n elements to two TreeSets, which takes O(nlogn) time complexity, and then finding the Intersection using retainAll Method, which takes O(n) time complexity on average.

The overall space complexity of the findIntersection Method is O(n) because it involves creating three sets of size n, which takes O(n) space complexity.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA