first belongs to one friend, second to the other. Position 0 holds the favorite.
For a place that appears in both lists, add up its two positions: its index in first plus its index in second. The smaller the sum, the better the compromise.
Return every shared place with the smallest position sum, in any order.first=['Pizza', 'Sushi', 'Tacos', 'Ramen'], second=['Ramen', 'Burgers', 'Sushi'] → ['Sushi', 'Ramen']
The shared places are "Sushi" (1 + 2 = 3) and "Ramen" (3 + 0 = 3). The sums tie, so both qualify.
first=['Bakery', 'Deli', 'Noodle Bar'], second=['Deli', 'Noodle Bar'] → ['Deli']
"Deli" gives 1 + 0 = 1 and "Noodle Bar" gives 2 + 1 = 3, so "Deli" is the only answer.
first=['Grill'], second=['Cafe', 'Grill'] → ['Grill']
There is just one shared place, so it is the answer.
first=['Pizza', 'Sushi', 'Tacos', 'Ramen'], second=['Ramen', 'Burgers', 'Sushi']
['Sushi', 'Ramen']
Run checks the first 3 tests in your browser. Submit checks all 90 tests on the server, hidden ones included.