-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathUnionUnsortedArray.java
52 lines (43 loc) · 1.22 KB
/
UnionUnsortedArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* Program to find the union of two unsorted array */
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class UnionUnsortedArray {
public static void main() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter n:");
int n = sc.nextInt();
int[] array1 = new int[n];
int[] array2 = new int[n];
System.out.println("Enter array1 elements:");
for(int i = 0 ; i < n ; i++)
array1[i] = sc.nextInt();
System.out.println("Enter array1 elements:");
for(int i = 0 ; i < n ; i++)
array2[i] = sc.nextInt();
unionArray(array1,array2);
}
static void unionArray(int[] array1, int[] array2) {
Map<Integer, Integer> map = new HashMap<Integer,Integer>();
// insert elements of array1 in the map
for(int i = 0 ; i < array1.length ; i++)
map.put(array1[i], i);
// insert elements of array1 in the map
for(int i = 0 ; i < array2.length ; i++)
map.put(array2[i], i);
System.out.print("Union of two unsorted array : ");
for(Map.Entry mapElement : map.entrySet())
System.out.print(mapElement.getKey() + " ");
}
}
/*
Input:
Enter n:
4
Enter array1 elements:
9 -3 -6 0
Enter array2 elements:
-1 -3 -6 1
Output:
Union of two unsorted array : 0 -1 1 -3 -6 9
*/