-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (48 loc) · 1.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// score: 140
function organizeChristmasDinner(dishes) {
const ingredients = {};
for (let [dish, ...dishIngredients] of dishes) {
for (let ingredient of dishIngredients) {
ingredients[ingredient]
? ingredients[ingredient].push(dish)
: ingredients[ingredient] = [dish];
}
}
let result = [];
for (let ingredient in ingredients) {
ingredients[ingredient].length > 1
? result.push([ingredient, ...ingredients[ingredient].sort()])
: '';
}
result.sort((a, b) => a[0].localeCompare(b[0]));
return result;
}
// score: 140
// function organizeChristmasDinner(dishes) {
// const ingredients = {};
// for (const [dish, ...dishIngredients] of dishes) {
// for (const ingredient of dishIngredients) {
// if (ingredients[ingredient]) {
// ingredients[ingredient].push(dish);
// } else {
// ingredients[ingredient] = [dish];
// }
// }
// }
// const result = [];
// for (const ingredient in ingredients) {
// if (ingredients[ingredient].length > 1) {
// result.push([ingredient, ...ingredients[ingredient].sort()]);
// }
// }
// result.sort((a, b) => a[0].localeCompare(b[0]));
// return result;
// }
module.exports = organizeChristmasDinner;
const dishes = [
["christmas turkey", "turkey", "sauce", "herbs"],
["cake", "flour", "sugar", "egg"],
["hot chocolate", "chocolate", "milk", "sugar"],
["pizza", "sauce", "tomato", "cheese", "ham"],
];
console.log(organizeChristmasDinner(dishes));