Javascript single loop solution with sets and another using 2 loops
Didn\'t see a Javascript solution with a set when I had a look around so sharing this one:\n\n\nconst destCity = function(paths) {\n  const departingCities = ne
Didn\'t see a Javascript solution with a set when I had a look around so sharing this one:\n\n```\nconst destCity = function(paths) {\n  const departingCities = new Set();\n  \n  paths.forEach((trip) => {\n    departingCities.add(trip[0]); // adds departing city to the set\n    };\n  }\n  \n  for (const trips of paths) {\n    if (!departingCities.has(trips[1]) { // checks to see if destination is a departure city, if not (!) then...\n\t  return trips[1]; // that destination must be the final destination, and therfore the solution\n\t}\n  }\n  \n  return;\n};\n```\nO(2n) time, O(n) space.\n\nI saw a couple of solutions use arrays to track and then .includes to look up. .includes is a linear operation, it is much faster to use a set because a set has constant lookup time. We do not need to use an object (hashmap) because we don\'t need to store anything meaningful in the value.\n\nWe also do not need to track the destinations in a separate variable because the destinations are already available in the paths input, by simply looping through again and comparing the destination city to see if it is already in the list of departing cities. If there is no departure from a destination city, that logically that must be the final city.\n\nFirst loop is a forEach, however second one cannot be because forEach doesn\'t return a value.\n\nNormally, I would not use a for of loop in the second loop. I used it only to make the code look cleaner and hopefully easier to read. I\'ve been discouraged from using for of loops but not exactly sure why so if anybody knows, please feel free to share!\n\nWas curious if this could be done in 1 loop...and well, there is but... meh\n\n```\nconst destCity = function(paths) {\n  const finalDest = new Set(); // candidates for final solution\n  const seenCities = new Set(); // all cities seen in paths so far\n\n  paths.forEach((path) => {\n    if (finalDest.has(path[0])) { // if current departure city is already candidate..\n      finalDest.delete(path[0]); // ...it is no longer a candidate\n    } else {\n      seenCities.add(path[0]);\n    }\n    if (!seenCities.has(path[1])) { // if current destination city is not already seen...\n      finalDest.add(path[1]); // ...it is added as a candidate\n      seenCities.add(path[1]);\n    }  \n  });\n\n  return finalDest.values().next().value;\n};\n```\n\nThe core concept behind this is that every city will be seen twice except 2 of them - the very starting city and the very last city. So if a city is listed in the paths only once and is in position [1] (a destination city) it is the final destination and solution. So basically this solution tracks every city seen and if a destination has never been seen before, it is added to a list of candidates. If and when a final destination candidate is seen again, that means we are departing to a new city, so that candidate can no longer be the solution, so it is deleted from the list of candidates. At the end, there will only be one candidate left.
['Ordered Set', 'JavaScript']