Skip to content

Instantly share code, notes, and snippets.

@basekays
Created November 21, 2018 21:05
Show Gist options
  • Save basekays/8df2279881b896205fbfc837704f1022 to your computer and use it in GitHub Desktop.
Save basekays/8df2279881b896205fbfc837704f1022 to your computer and use it in GitHub Desktop.
const findChildNode = function(array) {
const index = {};
let target;
for (let i = 0; i < array.length; i++) {
const parentNode = array[i][0];
const childNode = array[i][1];
if (index[childNode]) {
index[childNode].push(parentNode);
} else {
index[childNode] = [parentNode];
}
}
for (let childNode in index) {
if (index[childNode].length == 1) {
target = childNode;
}
}
return target;
}
findChildNode([[0, 1],[1, 3],[2, 3],[3, 5],[5, 9],[4, 5],[7, 1]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment