To find new friends in social network program

To find new friends in social network program

Question

This is a Social Network Program. Given a friend’s name and list of her friends, find new friends and skip mutual friends. Input and output should be like one mentioned below.

Social Network Progam:

  • Social network program finds the people who have mutual friends like Facebook.
  • The first line determines the number of current friends.
  • In the second line, “mani” is current friend name followed by the count of mani’s friend(3). After that “Ram raj guna” are mani’s friends. 
  • Same applies for the following next two line (strings).
  • Add the mutual friend’s name to friends list if not present already.

Algorithm

  1.  Create Set and add the current friends’ name.
  2.  Check each string in line and skip if it is number
  3.   If it is a character string add that to set.
  4.   If the adding operation is successful, print the friend name.

Program

[code lang=”java”]
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Wisdom {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int in,i;
char c;
String str[] = new String[10];
String a[][] = new String[10][10];
Set<String> frd = new HashSet();

System.out.print("enter no. of input");
in =sc.nextInt();
sc.nextLine(); // whenever nextLine() is used after nextInt() add this to consume the ‘enter’

for(i=0;i<in;i++)
str[i] = sc.nextLine(); //input line like’Mani 3 Ram Raj Guna’

for(i=0;i<in;i++)
a[i]= str[i].split("\\s+"); //splitting the input line to seperate strings

for(i=0;i<in;i++)
frd.add(a[i][0]); // finding the first element in the i’th row of two dimensional string

for(i=0;i<in;i++)
{

int j,n=a[i].length;

for(j=0;j<n;j++)
{
c = a[i][j].charAt(0); // first letter of string
if(Character.isLetter(c)) // whether character
{
if(frd.add(a[i][j])) // returns true if the element is not already present in set
System.out.print(a[i][j]+" ");
}
}
}
}
}
[/code]

You might also like…

Count of each character in a string

Ref: Facebook

Sree Hari Sanjeev

The founder of Wisdom Overflow. Software Developer at Zoho Corporation.

Leave a Reply

Your email address will not be published. Required fields are marked *