Random java programs: Intersection of two arrays.

               Arrays are the great source of storing data elements. Let us find the intersection of two arrays in java.

Logic:

Efficient approach to implement this concept is ‘Hashset’. Let us store the data of first array in Hashset and checks the data in the second array. the common elements are displayed as output.

Java implementation of intersection of two arrays:

The java implementation starts by including built in packages.

  • A public class is created with main() function.
  • Scanner object is created to read the input from the user.
  • It reads two array elements from the user.
  • An intersection function is created.
  • This function gets two arrays as input.
  • It creates two hashsets. Add the first array elements into first hashset.
  • The second array elements are checked with the hashset. If both are matched, it adds the number to the intersection hashset.
  • After the final elements, it displays the intersection elements value.

Program:

import java.util.HashSet;

import java.util.Set;

import java.util.Scanner;

public class ArrayIntersectionEg {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int i;

        System.out.print("Enter the first array elements with spaces: ");

        String input1 = scanner.nextLine();

      // Let us split the input string by spaces

        String[] arr1 = input1.split(" ");

     // Convert the string into an integer array

        int[] intArr1 = new int[arr1.length];

        for (i = 0; i < arr1.length; i++) {

            intArr1[i] = Integer.parseInt(arr1[i]);

        }

       System.out.print("Enter the second array elements with spaces: ");

        String input2 = scanner.nextLine();

        // let us split the input string by spaces

        String[] arr2 = input2.split(" ");

        // Convert the string to an integer array

        int[] intArr2 = new int[arr2.length];

        for (i = 0; i < arr2.length; i++) {

            intArr2[i] = Integer.parseInt(arr2[i]);

        }

     Set<Integer> intersec = findIntersec(intArr1, intArr2);

     System.out.println("The Intersection of two arrays are: " + intersec);

    }

    public static Set<Integer> findIntersec(int[] intArr1, int[] intArr2) {

        Set<Integer> set1 = new HashSet<>();

        Set<Integer> intersec = new HashSet<>();

        for (int no : intArr1) {

            set1.add(no);

        }

        for (int no : intArr2) {

            if (set1.contains(no)) {

                intersec.add(no);

            }

        }

        return intersec;

    }

}

Output:

C:\raji\blog>javac ArrayIntersectionEg.java

C:\raji\blog>java ArrayIntersectionEg

Enter the first array elements with spaces: 23 34 45 56 67 78

Enter the second array elements with spaces: 89 67 23 41 90 10

The Intersection of two arrays are: [67, 23]

This is the way of finding the intersection of two arrays was implemented in java. Hope this code segment is useful to you. Keep coding.

No comments:

Post a Comment