In the previous article, we have seen Java Program to Find the Intersection of Two Arrays of Integers
In this article we are going to see how to find Union of multiple arrays using Java programming language.
Java Program to Find the Union of Multiple Arrays
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to find the union of multiple arrays of integers.
Method-1: Java Program to Find the Union of Multiple Arrays By Static Initialization of Array Elements
Approach:
- Initialize a list of lists.
- Create a HashSet.
- Loop over the list of lists and the element of each list into the HashSet.
- Return the HashSet.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // create list of lists List<List<Integer>> listOfLists = new ArrayList<>(); listOfLists.add(Arrays.asList(1, 2, 3, 4, 5)); listOfLists.add(Arrays.asList(2, 3, 5)); listOfLists.add(Arrays.asList(1, 12, 24, 5)); listOfLists.add(Arrays.asList(23, 15, 24)); listOfLists.add(Arrays.asList(12, 15, 23)); // print the lists System.out.println("Union of the given arrays is: " + union(listOfLists)); } static Set<Integer> union(List<List<Integer>> listOfLists) { Set<Integer> union = new HashSet<>(); for (List<Integer> list : listOfLists) { union.addAll(list); } return union; } }
Output: Union of the given arrays is: [1, 2, 3, 4, 5, 23, 24, 12, 15]
Method-2: Java Program to Find the Union of Multiple Arrays By Dynamic Initialization of Array Elements
Approach:
- Initialize a List of Lists.
- Create scanner class object.
- Ask the user for number of arrays.
- Ask the user to enter the size of each individual list and the its elements one by one.
- Create a HashSet.
- Loop over the list of lists and the element of each list into the HashSet.
- Return the HashSet.
Program:
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { // create list of lists List<List<Integer>> listOfLists = new ArrayList<>(); Scanner sc = new Scanner(System.in); System.out.print("Enter the number of arrays: "); int n = sc.nextInt(); // take input from user for (int i = 0; i < n; i++) { System.out.print("Enter the size of array: "); int size = sc.nextInt(); List<Integer> list = new ArrayList<>(); System.out.print("Enter array elements: "); for (int j = 0; j < size; j++) { int element = sc.nextInt(); list.add(element); } listOfLists.add(list); } // print the lists System.out.println("Union of the given arrays is: " + union(listOfLists)); } static Set<Integer> union(List<List<Integer>> listOfLists) { Set<Integer> union = new HashSet<>(); for (List<Integer> list : listOfLists) { union.addAll(list); } return union; } }
Output: Enter the number of arrays: 4 Enter the size of array: 3 Enter array elements: 1 2 3 Enter the size of array: 4 Enter array elements: 2 3 4 5 Enter the size of array: 5 Enter array elements: 1 2 3 4 5 6 7 Enter the size of array: 6 Enter array elements: 5 6 7 8 9 0 Union of the given arrays is: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Related Java Programs:
- Java Program to Sort String Elements in an Alphabetical Order
- Java Program to Increment Each Element of Array by 1 and Print the Incremented Array
- Java Program to Decrement Each Element of Array by 1 and Print the Decremented Array
- Java Program to Decrement Each Element of Array by a Specified Number