I have an array of objects storing users which have Manager as a key. I want to find lowest Manager for any of the two user ids in the list.The number of objects would be large, hence looking for an efficient solution. For Example, for below list,
[
{
"User": "John",
"Manager": "Jake"
},
{
"User": "Ben",
"Manager": "John"
},
{
"User": "Steve",
"Manager": "John"
}...
]
The common Manager for Ben & Steve would be John. However, for Ben & John would be Jake.
My initial thoughts include converting list to a tree & then finding the lowest common ancestor, but I suspect there probably is a better approach.
@ShihabRahman's now deleted answer is efficient but didn't quite work since users, not managers, were considered part of the path. Make managers part of the path instead and it would work:
This outputs:
Demo: https://replit.com/@blhsing/ScientificHumongousGigabyte