Find Common Elements in Three Sorted Arrays in Java



The common elements in three sorted arrays are those elements that occur in all three of them. In this article, we will learn how to find common elements from three sorted arrays in Java. An example of this is given as follows ?

Example Scenario:

Input 1: arr1 = [1, 3, 5, 7, 9]
Input 2: arr2 = [2, 3, 6, 7, 9]
Input 3: arr3 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Output: Common elements = 3 7 9

Here, arrays are data structure which stores a fixed-size sequential collection of elements of the same data type.

Using Iteration

Here, we use the while and if-else-if statement to find common elements from three sorted arrays.

Run while loop till the length of each arrays. Inside this loop, define if-else-if statement which will check the common element and increment the loop variable by 1.

Example

A program that demonstrates this is given as follows ?

public class Example {
   public static void main(String args[]) {
      int arr1[] = {1, 4, 25, 55, 78, 99};
      int arr2[] = {2, 3, 4, 34, 55, 68, 75, 78, 100};
	  int arr3[] = {4, 55, 62, 78, 88, 98};
	  int i = 0, j = 0, k = 0, x = 0;
	  System.out.print("Array1: ");
	  for(x = 0; x < arr1.length; x++) {
	     System.out.print(arr1[x] + " ");
	  }
	  System.out.println();
	  System.out.print("Array2: ");
	  for(x = 0; x < arr2.length; x++) {
	     System.out.print(arr2[x] + " ");
	  }
	  System.out.println();
	  System.out.print("Array3: ");
	  for(x = 0; x < arr3.length; x++) {
	     System.out.print(arr3[x] + " ");
	  }
	  System.out.println();
	  System.out.println("The common elements in the 3 sorted arrays are: ");
	  while (i < arr1.length && j < arr2.length && k < arr3.length) {
	     if (arr1[i] == arr2[j] && arr2[j] == arr3[k]) {
		    System.out.print(arr1[i] + " ");
			i++;
			j++;
			k++;
	     } else if (arr1[i] < arr2[j]) {
			i++;
		 } else if (arr2[j] < arr3[k]) {
			j++;
		 } else {
		    k++;
		 }
	  }
   }
}

Output of the above code is as shown below ?

Array1: 1 4 25 55 78 99
Array2: 2 3 4 34 55 68 75 78 100
Array3: 4 55 62 78 88 98
The common elements in the 3 sorted arrays are: 4 55 78

Using Binary Search

In this approach, we use the binary search algorithm to find common elements in three sorted arrays.

  • Compare the middle element of the array with current element. If they are equal, the algorithm returns TRUE.
  • If current element is greater than the middle element, the search continues in the right half, otherwise, it continues in the left half.
  • If the element is not found, the method returns FALSE.

Example

Following is the Java program that illustrates the above discussed approach ?

import java.util.*;
public class Example {
   public static void main(String[] args) {
      int[] arr1 = {1, 4, 25, 55, 78, 99};
      int[] arr2 = {1, 3, 5, 78, 99};
      int[] arr3 = {99, 55, 62, 78, 88, 98};
      // Sorting arrays
      Arrays.sort(arr1);
      Arrays.sort(arr2);
      Arrays.sort(arr3);
      // storing common elements in a list
      List<Integer> commonList = findCommonElements(arr1, arr2, arr3);
      System.out.println("Common elements in the 3 sorted arrays are: " + commonList);
   }
   // method to find common elements from sorted arrays
   public static List<Integer> findCommonElements(int[] arr1, int[] arr2, int[] arr3) {
      List<Integer> result = new ArrayList<>();
      for (int num : arr1) {
         if (searching(arr2, num) && searching(arr3, num)) {
            result.add(num);
         }
      }
      return result;
   }
   // method of binary search
   public static boolean searching(int[] array, int num) {
      int n1 = 0;
      int n2 = array.length - 1;
      while (n2 >= n1) {
         int mid = (n1 + n2) / 2;
         if (array[mid] == num) {
            return true;
         } else if (array[mid] < num) {
            n1 = mid + 1;
         } else {
            n2 = mid - 1;
         }
      }
      return false;
   }
}

Output of the above code is obtained as ?

Common elements in the 3 sorted arrays are: [78, 99]
Updated on: 2024-08-16T07:29:57+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements