Using lodash, I have created an array of pairs, each representing source and destination of a link. So for a given path of links, I have:
[ [a, b], [a, c], [d, c] ]
It is a given that the first element is the first link in the chain.
I originally wanted to map the pairs into key/value map object and then go through the keys and/or values to find the head.
keys.contains(a) || values.contains(a)
if either returns true (excluding the pair itself, naturally), then it is not the head.
So, starting with the first pair, keys.contains(a) || values.contains(a) returns true so it is not the head, and keys.contains(b) || values.contains(b) returns false - so it must be the head.
The problem: I can't map the pairs into an object, since there might be key/value duplicates that override each other.
i.e., for the examples above, the resulting object will be:
I know I can loop through them, however I am reluctant to do so many loops unless I have to...
Any ideas of a more efficient solution?