Find Common Elements in Three Sorted Arrays – Java Code

Write a java program to find common elements in three sorted arrays. Given three sorted arrays, write a code to print intersection of three sorted arrays.

For example –

arr1 = {1, 5, 10, 20, 40, 80};
arr2 = {6, 7, 20, 80, 100};
arr3 = {3, 4, 15, 20, 30, 70, 80, 120};

Output : {20, 80}

20 and 80 is the common element between three sorted arrays.

Find intersection of two arrays

Find common elements in three sorted arrays

Find common elements in three sorted arrays

Method 1: Beginner’s Approach

Subscribe Our Tutorials

Get Latest Updates on Facebook

In this example, we are using three for loop to find common elements in three sorted arrays. This approach is only for demonstration purpose. The time complexity of this approach is O(n^3).

This problem can be solved in a single traversal instead of using three for loops.

Java Program to Find Common Elements in Three Sorted Arrays

In this approach we use single traversal to find intersection of three sorted arrays. The time complexity of this approach is O(N1 + N2 + N3).

I have explained both the approaches to solve this problem using video tutorial.Video tutorial link is present at the end of this tutorial.

 

In this video tutorial, I have explained two approaches to find common elements in three sorted arrays.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , , . Bookmark the permalink.