Find Union and Intersection of two unsorted arrays | Java program for the above approach ; Defining map container mp ; Inserting array elements in mp ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void printUnion ( int [ ] a , int n , int [ ] b , int m ) { Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mp . put ( a [ i ] , i ) ; } for ( int i = 0 ; i < m ; i ++ ) { mp . put ( b [ i ] , i ) ; } System . out . println ( " The ▁ union ▁ set ▁ of ▁ both ▁ arrays ▁ is ▁ : " ) ; for ( Map . Entry mapElement : mp . entrySet ( ) ) { System . out . print ( mapElement . getKey ( ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 5 , 6 , 2 , 3 , 5 } ; int b [ ] = { 2 , 4 , 5 , 6 , 8 , 9 , 4 , 6 , 5 } ; printUnion ( a , 7 , b , 9 ) ; } }