June 23, 2022

Java Program to Find Common Element Between Two Arrays

In this post we’ll see a Java program to find common elements between two arrays. For writing this program you may be asked not to use any inbuilt Java method, in that case you can iterate the arrays using for loop and list the common elements.

If it is permitted to use any inbuilt method then you can use retainAll() method of the Set in Java to get the common elements between two arrays.

Finding common elements between two arrays – Iterative

In this solution you can iterate one of the array in an outer loop and compare each element with all the elements of another array using an inner loop.

public class CommonElement {
  public static void main(String[] args) {
    int[] arr1 = {3, 10, 1, 0, 9};
    int[] arr2 = {32, 5, 10, 6, 9, 1};
    for(int i = 0; i < arr1.length; i++){
      for(int j = 0; j < arr2.length; j++){
        if(arr1[i] == arr2[j]){
          System.out.print(arr1[i] + " ");
          break;
        }
      }
    }
  }
}
Output
10 1 9

Finding common elements between two sorted arrays

Above solution is O(N2), if you can sort the arrays then you can reduce the time to O(2NLogN + N).

After sorting the arrays you compare elements of the arrays in a while loop and increment only one of the array index if the elements are not equal otherwise increment index in both the arrays.

public class CommonElement {
  public static void main(String[] args) {
    int[] arr1 = {6, 8, 1, 0, 9};
    int[] arr2 = {34, 2, 1, 9, 12, 67, 0};
    findCommonElement(arr1, arr2);
  }
  public static void findCommonElement(int[] arr1, int[] arr2){
    // sort arrays
    Arrays.sort(arr1);
    Arrays.sort(arr2);
    int lengthArr1 = arr1.length;
    int lengthArr2 = arr2.length;
    int i = 0, j = 0;
    while(i < lengthArr1 && j < lengthArr2){
      // compare and increment
      if(arr1[i] > arr2[j]){
        j++;
      }else if (arr2[j] > arr1[i]){
        i++;
      }else{
        //Print common element
        System.out.print(arr1[i] + " ");
        i++;
        j++;
      }
    }
  }
}
Output
0 1 9

Finding common elements between two arrays using HashSet

You can also use the retainAll() method of the HashSet to find the common elements between two arrays.

retainAll() method retains only those elements of the Set that are contained in the passed Collection.

public class CommonElement {

  public static void main(String[] args) {
    int[] arr1 = {3, 10, 1, 0, 9};
    int[] arr2 = {32, 5, 10, 6, 9, 1};
    findCommonElement(arr1, arr2);
  }
  public static void findCommonElement(int[] arr1, int[] arr2){
    Set<Integer> set1 = new HashSet<>();
    Set<Integer> set2 = new HashSet<>();
    // adding elements from array1
    for(int i : arr1){
      set1.add(i);
    }
    // adding elements from array2
    for(int i : arr2){
      set2.add(i);
    }
    set1.retainAll(set2);
    System.out.println("Common elements- " + set1);
  }
}
Output
Common elements- [1, 9, 10]

That's all for the topic Java Program to Find Common Element Between Two Arrays. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment