import java.io.*;
class union
{
public void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the size of array1 :- ");
int s1=Integer.parseInt(br.readLine());
System.out.print("Enter the size of array2 :- ");
int s2=Integer.parseInt(br.readLine());
int a[]=new int[s1];
int b[]=new int[s2];
int c[]=new int[s1+s2];
int i,j,pos1=0,pos2=0,pos3=0,f=0;
System.out.print("Enter elements of array1 ");
for(i=0;i<s1;i++)
a[i]=Integer.parseInt(br.readLine());
System.out.print("Enter elements of array2 ");
for(i=0;i<s2;i++)
b[i]=Integer.parseInt(br.readLine());
for(i=0;i<s1;i++)
{f=0;
for(j=0;j<i;j++)
{
if(a[i]==a[j])
{
f=1;
break;
}
}
if(f==0)
{
a[pos1++]=a[i];
c[pos3++]=a[i];
}
}
for(i=0;i<s2;i++)
{f=0;
for(j=0;j<i;j++)
{
if(b[i]==b[j])
{
f=1;
break;
}
}
if(f==0)
b[pos2++]=b[i];
}
for(i=0;i<pos2;i++)
{f=0;
for(j=0;j<pos1;j++)
{
if(b[i]==a[j])
{
f=1;
break;
}}
if(f==0)
c[pos3++]=b[i];
}
for(i=0;i<pos3;i++)
System.out.print(c[i]+" ");
}
}
No comments:
Post a Comment