From 3c14c811837f246fbfef6a11ec98e798ad6d4f3d Mon Sep 17 00:00:00 2001 From: Alejandro Medina <5almero5@gmail.com> Date: Mon, 8 Jun 2020 16:18:52 -0500 Subject: [PATCH] add Logical of function --- src/index.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 4101a36..60daeeb 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,23 @@ * @returns {number[][]} */ const findNodesWithZeroAndOneParents = (parentChildPairs) => { - + let parents = new Set(parentChildPairs.map(array => array[0])) + let childs = parentChildPairs.map(array => array[1]) + let grandPa = []; + let divorceChilds = new Set(); + parents.forEach(parent => { + if(childs.indexOf(parent) === -1){ + grandPa.push(parent) + } + }) + childs.forEach((child, index) => { + if(childs.indexOf(child) === index) { + divorceChilds.add(child) + }else if (divorceChilds.has(child)) { + divorceChilds.delete(child) + } + }) + return [grandPa, [... divorceChilds].sort((a, b) => a - b)] } module.exports = findNodesWithZeroAndOneParents; \ No newline at end of file