Suppose that you and I are friends on Facebook and you want
Solution
import java.util.*;
public class CommonFriend
{
public static void main(String ss[])
{
//Two friend ID array
int fID1[], fID2[];
System.out.println(\"\ Enter First Friend ID array length: \");
fID1 = readFriendIDs();
System.out.println(\"\ Enter Second Friend ID array length: \");
fID2 = readFriendIDs();
//Displays the contents of the first friend ID list
System.out.println(\"\ First Friend IDs : \");
for(int c = 0; c < fID1.length; c++)
System.out.print(fID1[c] + \" \");
//Displays the contents of the second friend ID list
System.out.println(\"\ Second Friend IDs : \");
for(int c = 0; c < fID2.length; c++)
System.out.print(fID2[c] + \" \");
//Find out the common friend
displayCommonFriendIDs(fID1, fID2);
}
//Returns the ID list after accepting data
public static int[] readFriendIDs()
{
int no1, t, f = 0;
//Scanner calss object created to accept data
Scanner sc = new Scanner(System.in);
//Accepts the length of the array
no1 = sc.nextInt();
//Creates a temporaty array of specified length
int temp [] = new int[no1];
System.out.println(\"\ Enter Friend ID\");
for(int c = 0; c < no1; )
{
t = sc.nextInt();
//Restricts duplicate ID
for(int d = 0; d < c; d++)
{
if(t == temp[d])
{
f = 1; //Sets the flag to 1 if data is already available
System.out.println(\"\ Error: Duplicate ID not allowed: \");
break;
}
}
//If ID is not available in the list f value is not 1
//So store the valid ID
if(f == 0)
{
temp[c] = t;
c++;
}
//Reset the flag to 0
f = 0;
}
return temp;
}
//Checks and displays the common ID from two array
public static void displayCommonFriendIDs(int a[], int b[])
{
int f, s, c = 0, len;
//Calculates the length of both the array
int flen = a.length;
int slen = b.length;
if(flen > slen)
len = slen;
else
len = flen;
//Sets the common array length equal to the biggest length out of the two array
int comm[] = new int[len];
for(f = 0; f < flen; f++)
{
for(s = 0; s < slen; s++)
{
//If both array having common ID store it in common array
if(a[f] == b[s])
{
comm[c] = a[f];
c++;
}
}
}
//Displays the common array ID
System.out.println(\"\ There are \" + c + \" friends in common: IDs \");
for(f = 0; f < c; f++)
if(f < c - 1)
System.out.print(comm[f] + \", \");
else
System.out.print(comm[f] + \".\");
}
}
Output 1:
Enter First Friend ID array length:
5
Enter Friend ID
1
2
3
1
Error: Duplicate ID not allowed:
4
3
Error: Duplicate ID not allowed:
5
Enter Second Friend ID array length:
8
Enter Friend ID
11
22
33
1
5
11
Error: Duplicate ID not allowed:
44
55
66
First Friend IDs :
1 2 3 4 5
Second Friend IDs :
11 22 33 1 5 44 55 66
There are 2 friends in common: IDs
1, 5.
Output 2:
Enter First Friend ID array length:
10
Enter Friend ID
12
2
3
4
5
6
7
7
Error: Duplicate ID not allowed:
8
9
10
Enter Second Friend ID array length:
11
Enter Friend ID
22
33
12
44
2
3
6
66
10
7
5
First Friend IDs :
12 2 3 4 5 6 7 8 9 10
Second Friend IDs :
22 33 12 44 2 3 6 66 10 7 5
There are 7 friends in common: IDs
12, 2, 3, 5, 6, 7, 10.



