Similar Problems

Similar Problems not available

Friend Requests Ii Who Has The Most Friends - Leetcode Solution

Companies:

LeetCode:  Friend Requests Ii Who Has The Most Friends Leetcode Solution

Difficulty: Medium

Topics: database  

Problem Statement:

You have a social networking website where people can create profiles, connect with friends, and share posts. Given a list of user profiles, each containing a user ID and a list of friends, find the user(s) with the most friends.

Note: Each user's friend list is unique and does not contain duplicates.

Example:

Input:

{ "user1": ["user2", "user3"], "user2": ["user1", "user4", "user5"], "user3": ["user1", "user6"], "user4": ["user2"], "user5": ["user2"], "user6": ["user3"] }

Output:

["user2"]

Explanation:

"user2" has the most friends, with 3 friends: "user1", "user4", and "user5". "user1" and "user3" both have two friends each, and "user4", "user5", and "user6" each have one friend.

Solution:

This problem can be solved by iterating through the given dictionary/hashmap of user profiles and friends, and counting the number of friends for each user. We can keep track of the maximum number of friends seen so far, and update it whenever we find a user with more friends than the previous maximum.

We can use a dictionary/hashmap to keep track of the number of friends for each user, and another variable to keep track of the maximum number of friends seen so far. We can iterate through the list of friends for each user and increment the number of friends for each friend in the dictionary. Finally, we can iterate through the dictionary and find the user(s) with the maximum number of friends, and return them.

Here's the Python code for the solution:

def most_friends(users): # Initialize a dictionary to keep track of the number of friends for each user friends_count = {}

# Initialize a variable to keep track of the maximum number of friends seen so far
max_friends = 0

# Iterate through the given dictionary of user profiles and friends
for user, friends in users.items():
    # Initialize the number of friends for the current user to 0
    friends_count[user] = 0

    # Iterate through the list of friends for the current user
    for friend in friends:
        # Increment the number of friends for the current friend in the dictionary
        friends_count[friend] = friends_count.get(friend, 0) + 1

    # Update the maximum number of friends seen so far if the current user has more friends
    if friends_count[user] > max_friends:
        max_friends = friends_count[user]

# Initialize a list to keep track of the users with the maximum number of friends
max_friends_users = []

# Iterate through the dictionary and find the user(s) with the maximum number of friends
for user, count in friends_count.items():
    if count == max_friends:
        max_friends_users.append(user)

return max_friends_users

Testing the function with example input

users = { "user1": ["user2", "user3"], "user2": ["user1", "user4", "user5"], "user3": ["user1", "user6"], "user4": ["user2"], "user5": ["user2"], "user6": ["user3"], } print(most_friends(users)) # ["user2"]

Friend Requests Ii Who Has The Most Friends Solution Code

1