Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,32 @@
* @returns {number[][]}
*/
const findNodesWithZeroAndOneParents = (parentChildPairs) => {
const result = [[],[]];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creo que pues omitir esta variable si regresas directamente return [ zeroParentsChildren, oneParentChildren];


const countOfParentsPerChild = parentChildPairs.reduce((dictionary, pair) => {
if (dictionary[pair[1]])
dictionary[pair[1]]++;
else
dictionary[pair[1]] = 1;
return dictionary;
}, {});


const oneParentChildren = Object.keys(countOfParentsPerChild)
.filter(child => countOfParentsPerChild[child] === 1)
.map(childKey => parseInt(childKey));

let zeroParentsChildren = Array.from(
new Set(
parentChildPairs.map(pair => pair[0])
.filter(child => !(child in countOfParentsPerChild))
)
);
Comment on lines +22 to +27
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!


result[0] = zeroParentsChildren;
result[1] = oneParentChildren;

return result;
}

module.exports = findNodesWithZeroAndOneParents;