Created
November 21, 2018 21:05
-
-
Save basekays/8df2279881b896205fbfc837704f1022 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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