Jeff的隨手筆記

學習當一個前端工程師

0%

用LeetCode寫日記-Day25

這兩天早上LeetCode都無法出現JavaScript30的題目,導致昨天一加班回家就來不及寫昨天的題目,只好改成今天寫。
明天再看看,如果真的都這樣,就要改完上寫了。

Day25: Join Two Arrays by ID

問題描述:

Given two arrays arr1 and arr2, return a new array joinedArray. All the objects in each of the two inputs arrays will contain an id field that has an integer value. joinedArray is an array formed by merging arr1 and arr2 based on their id key. The length of joinedArray should be the length of unique values of id. The returned array should be sorted in ascending order based on the id key.

If a given id exists in one array but not the other, the single object with that id should be included in the result array without modification.

If two objects share an id, their properties should be merged into a single object:

  • If a key only exists in one object, that single key-value pair should be included in the object.
  • If a key is included in both objects, the value in the object from arr2 should override the value from arr1.

問題難度:Medium

問題限制:

  • arr1 and arr2 are valid JSON arrays
  • Each object in arr1 and arr2 has a unique integer id key
  • 2 <= JSON.stringify(arr1).length <= 106
  • 2 <= JSON.stringify(arr2).length <= 106

我的解題過程:

題目的要求就是要我們合併2個array並回傳一個新的array,當有重複的key時就把重複的項目合併。

首先我們先創建一個新的array,並且把arr1的每個物件按照其 id 的值作為key,存入新的array。

1
2
3
4
5
6
7
8
const joinedArray = []
for (let i = 0; i < arr1.length; i++) {
let newKey = arr1[i].id
let newValue = arr1[i]
joinedArray.push({
[newKey] : newValue,
})
}

接下來就是遍歷arr2,如果有相同的id就加入,如果沒有就新增

1
2
3
4
5
6
7
8
9
10
for (let i = 0; i < arr2.length; i++) {
const currentId = arr2[i].id
if (currentId) {
joinedArray[currentId] = {...joinedArray[currentId], ...arr2[i]}
} else {
joinedArray.push({
[currentId] : arr2[i]
})
}
}

直接回傳結果直接噴錯

結果錯誤解了1個小時還是解不開,就只好去看答案,想法沒有太多出路但有一個語法我不知道:**Object.values()**

The Object.values() static method returns an array of a given object’s own enumerable string-keyed property values.

直接看範例會比較清楚:

1
2
3
4
5
6
7
8
const object1 = {
a: 'somestring',
b: 42,
c: false,
};

console.log(Object.values(object1));
// Expected output: Array ["somestring", 42, false]

因此我其實不需要用變數是array,可以使用object然後再用這個method 把它轉換成array。
果然這樣一改3個測試就過了!

但有隱藏版的第4個測試,那個就沒過…
詢問GPT 給的答覆是:

你的程式碼在處理 else 分支時,有使用 currentId 這個可能為 undefined 的值作為物件的 key,這可能導致不符合預期的結果。為了處理這種情況,我們可以避免使用 currentId 作為 key,可以考慮使用一個特定的 key 來處理沒有有效 id 的情況。

所以稍微做的更改!

我的code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var join = function(arr1, arr2) {
const joinedArray = {};

for (let i = 0; i < arr1.length; i++) {
const newKey = arr1[i].id;
const newValue = arr1[i];
joinedArray[newKey] = newValue;
}

for (let i = 0; i < arr2.length; i++) {
const currentId = arr2[i].id;
if (currentId !== undefined) {
joinedArray[currentId] = { ...joinedArray[currentId], ...arr2[i] };
} else {
const undefinedKey = 'undefinedKey';
joinedArray[undefinedKey] = { ...joinedArray[undefinedKey], ...arr2[i] };
}
}

return Object.values(joinedArray);
};

其他解法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var join = function(arr1, arr2) {
const result = {};
for (let i = 0; i < arr1.length; i++) {
result[arr1[i].id] = arr1[i];
}
for (let i = 0; i < arr2.length; i++) {
if (result[arr2[i].id]) {
for (const key in arr2[i]) result[arr2[i].id][key] = arr2[i][key];
} else {
result[arr2[i].id] = arr2[i];
}
}

return Object.values(result);
};

結論:

2個小時…早就超過停損點3次的時間了,真的之後番茄時鐘要開,不然真的會陷下去…