Find Root of N-Ary Tree

An N-ary tree is stored as a set of nodes whose values are unique non-negative integers. Two arrays describe it. parentValues holds the value of every node in the tree, including the root, in arbitrary order. childValues holds the values of all children, flattened across every node's child list, so a node that appears as somebody's child contributes exactly one entry there per parent edge. The tree is well formed: exactly one node is not anybody's child, and that node is the root.

Report the value of the root.

Because every non-root node sits in both arrays exactly once, matching entries cancel in pairs while the root has no partner. Bitwise exclusive or over the concatenation of both arrays therefore leaves precisely the root value: duplicates vanish, and the single unpaired value survives. The scan visits each entry once and keeps one accumulator, so the cost is linear time with constant extra memory, which is stronger than the usual hash-set solution that stores every seen value. Values must be non-negative and unique; on such input the procedure is exact for any tree shape, including a chain and a single-node tree where childValues is empty.

Examples
Input: [[1,2,3,4],[2,3,4]]
Output: 1
Hints

Find Root of N-Ary Tree

An N-ary tree is stored as a set of nodes whose values are unique non-negative integers. Two arrays describe it. `parentValues` holds the value of every node in the tree, including the root, in arbitrary order. `childValues` holds the values of all children, flattened across every node's child list, so a node that appears as somebody's child contributes exactly one entry there per parent edge. The tree is well formed: exactly one node is not anybody's child, and that node is the root.