[JavaScript] Union Find 2 Solutions
javascript\nvar friendRequests = function(numPeople, restrictions, requests) {\n    // base union find data structure\n    const friendships = [...new Array(num
```javascript\nvar friendRequests = function(numPeople, restrictions, requests) {\n    // base union find data structure\n    const friendships = [...new Array(numPeople).keys()];\n    // this is equivalent to find in union find\n    const findRootFriend = (x) => friendships[x] = friendships[x] === x ? x : findRootFriend(friendships[x]);\n    // this is equivalent to union in union find\n    const createFriendship = (x, y) => friendships[findRootFriend(x)] = findRootFriend(y);\n    \n    const isInSameFriendshipGroup = (person1, person2) => findRootFriend(person1) === findRootFriend(person2);\n    const findFriendsInGroup = (friend) => {\n        const friendsInGroup = [];\n        for (let person = 0; person < numPeople; person++) {\n            if (isInSameFriendshipGroup(person, friend)) {\n                friendsInGroup.push(person);\n            }\n        }\n        return friendsInGroup;\n    }\n    \n    const mappedRestrictions = mapRestrictions(numPeople, restrictions);\n    \n    return requests.map(([friend1, friend2], i) => {\n        const friendsOfFriend1 = findFriendsInGroup(friend1);\n        \n        for (let person = 0; person < numPeople; person++) {\n            if (isInSameFriendshipGroup(person, friend2)) {\n\n                // check all the friends in friend1\'s group to see if\n                // person can be friends with friend of friend1\n                for (let friendOfFriend1 of friendsOfFriend1) {\n                    if (mappedRestrictions[person].has(friendOfFriend1)) {\n                        return false;\n                    }\n                }\n            }\n        }\n        // create the friendship\n        createFriendship(friend1, friend2);\n        return true;\n    })\n};\n\nfunction mapRestrictions(numPeople, restrictions) {\n    const mappedRestrictions = new Array(numPeople).fill(0).map(() => new Set());\n    for (const [person1, person2] of restrictions) {\n        mappedRestrictions[person1].add(person2);\n        mappedRestrictions[person2].add(person1);\n    }\n    return mappedRestrictions;\n}\n\n\n\n//////////////////////////////////////////////////////////\n//////////////////////////////////////////////////////////\n//////////////////////////////////////////////////////////\n// Credit to: lee215\n\n\nvar friendRequests = function(numPeople, restrictions, requests) {\n    // base union find data structure\n    const friendships = [...new Array(numPeople).keys()];\n    // this is equivalent to find in union find\n    const findRootFriend = (x) => friendships[x] = friendships[x] === x ? x : findRootFriend(friendships[x]);\n    // this is equivalent to union in union find\n    const createFriendship = (x, y) => friendships[findRootFriend(x)] = findRootFriend(y);\n    const isInSameFriendshipGroup = (person1, person2) => findRootFriend(person1) === findRootFriend(person2);\n    \n    return requests.map(([friend1, friend2], i) => {\n        if (isInSameFriendshipGroup(friend1, friend2)) return true;\n        \n        for (const [person1, person2] of restrictions) {\n            if ((isInSameFriendshipGroup(person1, friend1) && isInSameFriendshipGroup(person2, friend2)) ||\n                (isInSameFriendshipGroup(person1, friend2) && isInSameFriendshipGroup(person2, friend1))) {\n                return false;\n            }\n        }\n        createFriendship(friend1, friend2);\n        return true;\n    })\n};\n```
['Union Find', 'JavaScript']