Union and Intersection of two sorted arrays | Java program to find union of two sorted arrays ( Handling Duplicates ) ; Taking max element present in either array ; Finding elements from 1 st array ( non duplicates only ) . Using another array for storing union elements of both arrays Assuming max element present in array is not more than 10 ^ 7 ; First element is always present in final answer ; Incrementing the First element ' s ▁ count ▁ ▁ in ▁ it ' s corresponding index in newtable ; Starting traversing the first array from 1 st index till last ; Checking whether current element is not equal to it 's previous element ; Finding only non common elements from 2 nd array ; By checking whether it 's already  present in newtable or not ; Driver Code
class FindUnion { static void UnionArray ( int arr1 [ ] , int arr2 [ ] ) { int m = arr1 [ arr1 . length - 1 ] ; int n = arr2 [ arr2 . length - 1 ] ; int ans = 0 ; if ( m > n ) { ans = m ; } else ans = n ; int newtable [ ] = new int [ ans + 1 ] ; System . out . print ( arr1 [ 0 ] + " ▁ " ) ; ++ newtable [ arr1 [ 0 ] ] ; for ( int i = 1 ; i < arr1 . length ; i ++ ) { if ( arr1 [ i ] != arr1 [ i - 1 ] ) { System . out . print ( arr1 [ i ] + " ▁ " ) ; ++ newtable [ arr1 [ i ] ] ; } } for ( int j = 0 ; j < arr2 . length ; j ++ ) { if ( newtable [ arr2 [ j ] ] == 0 ) { System . out . print ( arr2 [ j ] + " ▁ " ) ; ++ newtable [ arr2 [ j ] ] ; } } } public static void main ( String args [ ] ) { int arr1 [ ] = { 1 , 2 , 2 , 2 , 3 } ; int arr2 [ ] = { 2 , 3 , 4 , 5 } ; UnionArray ( arr1 , arr2 ) ; } }