Merge two sorted arrays | Java program to merge two sorted arrays using maps ; Function to merge arrays ; Declaring a map . using map as a inbuilt tool to store elements in sorted order . ; Inserting values to a map . ; Printing keys of the map . ; Driver Code ; Function call
import java . io . * ; import java . util . * ; class GFG { static void mergeArrays ( int a [ ] , int b [ ] , int n , int m ) { Map < Integer , Boolean > mp = new HashMap < Integer , Boolean > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mp . put ( a [ i ] , true ) ; } for ( int i = 0 ; i < m ; i ++ ) { mp . put ( b [ i ] , true ) ; } for ( Map . Entry < Integer , Boolean > me : mp . entrySet ( ) ) { System . out . print ( me . getKey ( ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 5 , 7 } , b [ ] = { 2 , 4 , 6 , 8 } ; int size = a . length ; int size1 = b . length ; mergeArrays ( a , b , size , size1 ) ; } }