DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

3 2

Minimum Index Sum of Two Lists

Minimum Index Sum of Two Lists

/**
 * @param {string[]} list1
 * @param {string[]} list2
 * @return {string[]}
 */
var findRestaurant = function (list1, list2) {
  let map = {};
  let minSum = Infinity;

  for (let i = 0; i < list1.length; i++) {
    if (list2.includes(list1[i])) {
      let indexInL1 = i;
      let indexInL2 = list2.indexOf(list1[i]);
      let sum = indexInL1 + indexInL2;
      minSum = Math.min(sum, minSum);
      map[list1[i]] = sum;
    }
  }

  let array = [];

  for (const [key, value] of Object.entries(map)) {
    if (value === minSum) {
      array.push(key);
    }
  }

  return array;
};

findRestaurant(
  ["Shogun", "Tapioca Express ", "Burger King", "KFC"],
  [
    "Piatti KFC",
    "The Grill at Torrey Pines",
    "Hungry Hunter Steakhouse",
    "Shogun",
  ]
);

findRestaurant(
  ["Shogun", "Tapioca Express", "Burger King", "KFC"],
  ["KFC", "Shogun", "Burger King"]
);

findRestaurant(["happy", "sad", "good"], ["sad", "happy", "good"]);

Enter fullscreen mode Exit fullscreen mode

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Quickstart image

Django MongoDB Backend Quickstart! A Step-by-Step Tutorial

Get up and running with the new Django MongoDB Backend Python library! This tutorial covers creating a Django application, connecting it to MongoDB Atlas, performing CRUD operations, and configuring the Django admin for MongoDB.

Watch full video →

👋 Kindness is contagious

If you found these insights valuable, a like or a brief comment would be much appreciated. Also, consider joining our vibrant community and sharing your perspective on DEV!

Join the Community