Search⌘ K

Solution to Find Mutual Friends

Explore how to integrate setup, mapping, grouping, and reducing functions into a MapReduce process in JavaScript. Understand how to transform an array of friends into mutual friend results through this simulation, completing a practical solution for finding mutual friends.

We'll cover the following...

Putting it all together

Now that all functions are working and ready, let’s jump into the final mapreduce function. This will be the simulation of our MapReduce framework. Look at the code below.

Node.js
// Final
function mapreduce (friends) {
var initial = setup(friends);
var mapped = mapper(initial);
var grouped = group(mapped);
var reduced = reducer(grouped);
return reduced;
// return an object {(man,friend): [Friends]}
}
var arr = [
['A', 'B'], ['A', 'C'],
['A', 'D'], ['B', 'C'],
['B', 'D'], ['B', 'E'],
['C', 'D'], ['C', 'E'],
['D', 'E']
];
console.log(mapreduce(arr));

The code above calls a series of functions in the ...