Skip to content

Instantly share code, notes, and snippets.

@rohan-paul
Created September 21, 2019 14:24
Show Gist options
  • Save rohan-paul/43e7b60301f1fde3b0d2d91d1c6a6176 to your computer and use it in GitHub Desktop.
Save rohan-paul/43e7b60301f1fde3b0d2d91d1c6a6176 to your computer and use it in GitHub Desktop.
var map = {
tkt1: {
departure: 'Los Angeles',
arrival: 'San Francisco'
},
tkt2: {
departure: 'San Francisco',
arrival: 'New York'
},
tkt3: {
departure: 'Moscow',
arrival: 'Mali'
},
tkt4: {
departure: 'Barcelona',
arrival: 'Moscow'
},
tkt5: {
departure: 'New York',
arrival: 'Barcelona'
}
};
findDepartureArrival = function(map) {
var hashMap = {};
for (var tkt in map) {
var depart = map[tkt].departure;
var arriv = map[tkt].arrival;
if (!(depart in hashMap))
hashMap[depart] = -1;
else
hashMap[depart] = hashMap[depart] - 1;
if (!(arriv in hashMap))
hashMap[arriv] = 1;
else
hashMap[arriv] = hashMap[arriv] + 1;
}
return hashMap;
}
console.log(findDepartureArrival(map));
/*Output
{ 'Los Angeles': -1,
'San Francisco': 0,
'New York': 0,
Moscow: 0,
Mali: 1,
Barcelona: 0 }*/
@conste11ations
Copy link

you are on a biz trip and travelling from one city to another. you have a stack of unsorted flight boarding passes. only departure city and destination city are on the boarding pass. how do you find the first departure city and your final destination city, write the solution in javascript.

@Alejogb1
Copy link

What does "var = tkt" means? Does it function like an index?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment