
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Union of Two Arrays in Java
In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.
As per the problem statement we have to find the union of two arrays. Union refers to joining together whereas union of arrays refers to the new array combining all unique elements from the input arrays.
Let's explore the article to see how it can be done by using Java programming language.
To Show You Some Instances
Instance-1
Suppose the first array is {0, 3, 5, 6, 9, 1}.
And the second array is {1, 2, 6, 10, 8, 7}.
Then after performing the union operation the result will be ?
Union of two arrays is: [0, 1, 2, 3, 5, 6, 7, 8, 9, 10]
Instance-2
Suppose the first array is {0, 5, 11, 7, 9, 3}.
And the second array is {1, 2, 4, 5, 12, 7}.
Then after performing the union operation the result will be ?
Union of two arrays is: [0, 1, 2, 3, 4, 5, 7, 9, 11, 12]
Instance-3
Suppose the first array is {12, 13, 5, 16, 9, 19}.
And the second array is {16, 2, 60, 9, 8, 5}.
Then after performing the union operation the result will be ?
Union of two arrays is: [16, 2, 19, 5, 8, 9, 12, 60, 13]
Algorithm
Step 1 ? Declare and initialize an integer array.
Step 2 ? Initialize Hashset to store the union of the first and second array.
Step 3 ? Add all elements of the first array and second array to set.
Step 4 ? Print the result.
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.
Below refers to the syntax of it ?
array.length
To convert an array into a list of elements Arrays class of java.util package provides inbuilt asList() method.
Below refers to the syntax of it ?
Arrays.asList(array);
Where, ?array' refers to the array reference.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Array.
By Using User Defined Method.
Let's see the program along with its output one by one.
Approach-1: By Using Static Initialization of Array
Example
In this approach, array elements will be initialized in the program. Then as per the algorithm find the union of two arrays.
import java.util.*; public class Main{ //main method public static void main(String[] args){ //Declare and initialize the first array elements Integer arr1[] = {12, 13, 5, 16, 9, 19}; //Declare and initialize the second array elements Integer arr2[] = {16, 2, 60, 9, 8, 5}; //Initialize Hashset to perform union operation HashSet<Integer> set = new HashSet<>(); //add first array to set set.addAll(Arrays.asList(arr1)); //add second array to set set.addAll(Arrays.asList(arr2)); //convert to array from set Integer[] union = {}; union = set.toArray(union); //print the result System.out.println("Union of two arrays is: " + Arrays.toString(union)); } }
Output
Union of two arrays is: [16, 2, 19, 5, 8, 9, 12, 60, 13]
Approach-2: By Using User Defined Method
Example
In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside the method as per the algorithm find the union of two arrays.
import java.util.*; public class Main{ //main method public static void main(String[] args){ //Declare and initialize the first array elements Integer arr1[] = { 0, 5, 11, 7, 9, 3}; //Declare and initialize the second array elements Integer arr2[] = { 1, 2, 4, 5, 12, 7 }; union(arr1, arr2); } //user defined method public static void union(Integer []arr1, Integer []arr2){ //Initialize Hashset to perform union operation HashSet<Integer> set = new HashSet<>(); //add first array to set set.addAll(Arrays.asList(arr1)); //add second array to set set.addAll(Arrays.asList(arr2)); //convert to array from set Integer[] union = {}; union = set.toArray(union); //print the result System.out.println("Union of two arrays is: " + Arrays.toString(union)); } }
Output
Union of two arrays is: [0, 1, 2, 3, 4, 5, 7, 9, 11, 12]
In this article, we explored how to find the union of two arrays in Java.