Skip to content

Instantly share code, notes, and snippets.

@richardba
Last active November 12, 2021 22:38
Show Gist options
  • Save richardba/2dc78a01a2ffdc3f70f30032d4c1f85f to your computer and use it in GitHub Desktop.
Save richardba/2dc78a01a2ffdc3f70f30032d4c1f85f to your computer and use it in GitHub Desktop.
function findZeroOrOneParentage(input)
{
var objects = {}
var set = new Set()
input.forEach(function(elem){
if(objects[elem[0]] === undefined)
objects[elem[0]] = []
if(objects[elem[1]] === undefined)
objects[elem[1]] = []
objects[elem[0]].push(elem[1])
set.add(elem[0]).add(elem[1])
})
var result = [[],[]]
set.forEach(function(value) {
if(objects[value].length === 0)
result[0].push(value)
else if(objects[value].length === 1)
result[0].push(value)
})
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment