{"ScriptPreparationCode":"const input = [\r\n undefined, // empty data\r\n { hash: \u0022a\u0022, amount: 1 },\r\n undefined, // empty data\r\n { hash: \u0022c\u0022, amount: 1 },\r\n { hash: \u0022b\u0022, amount: 3 },\r\n undefined, // empty data\r\n { hash: \u0022d\u0022, amount: 1 },\r\n { hash: \u0022b\u0022, amount: 1 },\r\n { hash: \u0022a\u0022, amount: 1 },\r\n undefined, // empty data\r\n { hash: \u0022f\u0022, amount: 3 },\r\n { hash: \u0022c\u0022, amount: 1 },\r\n { hash: \u0022d\u0022, amount: 1 },\r\n { hash: \u0022a\u0022, amount: 1 },\r\n undefined, // empty data\r\n { hash: \u0022e\u0022, amount: 3 },\r\n { hash: \u0022c\u0022, amount: 1 },\r\n undefined, // empty data\r\n { hash: \u0022k\u0022, amount: 1 },\r\n undefined, // empty data\r\n { hash: \u0022s\u0022, amount: 1 },\r\n { hash: \u0022b\u0022, amount: 3 },\r\n undefined, // empty data\r\n { hash: \u0022d\u0022, amount: 1 },\r\n { hash: \u0022c\u0022, amount: 1 },\r\n { hash: \u0022a\u0022, amount: 1 },\r\n undefined, // empty data\r\n { hash: \u0022f\u0022, amount: 3 },\r\n { hash: \u0022c\u0022, amount: 1 },\r\n { hash: \u0022d\u0022, amount: 1 },\r\n { hash: \u0022a\u0022, amount: 1 },\r\n undefined, // empty data\r\n { hash: \u0022e\u0022, amount: 3 },\r\n { hash: \u0022c\u0022, amount: 1 },\r\n undefined, // empty data\r\n { hash: \u0022a\u0022, amount: 1 },\r\n undefined, // empty data\r\n { hash: \u0022c\u0022, amount: 1 },\r\n { hash: \u0022s\u0022, amount: 3 },\r\n undefined, // empty data\r\n { hash: \u0022d\u0022, amount: 1 },\r\n { hash: \u0022b\u0022, amount: 1 },\r\n { hash: \u0022a\u0022, amount: 1 },\r\n undefined, // empty data\r\n { hash: \u0022h\u0022, amount: 3 },\r\n { hash: \u0022c\u0022, amount: 1 },\r\n { hash: \u0022d\u0022, amount: 1 },\r\n { hash: \u0022a\u0022, amount: 1 },\r\n undefined, // empty data\r\n { hash: \u0022e\u0022, amount: 3 },\r\n { hash: \u0022c\u0022, amount: 1 },\r\n undefined, // empty data\r\n { hash: \u0022a\u0022, amount: 1 },\r\n undefined, // empty data\r\n { hash: \u0022n\u0022, amount: 1 },\r\n { hash: \u0022b\u0022, amount: 3 },\r\n undefined, // empty data\r\n { hash: \u0022d\u0022, amount: 1 },\r\n { hash: \u0022b\u0022, amount: 1 },\r\n { hash: \u0022e\u0022, amount: 1 },\r\n undefined, // empty data\r\n { hash: \u0022f\u0022, amount: 3 },\r\n { hash: \u0022c\u0022, amount: 1 },\r\n { hash: \u0022d\u0022, amount: 1 },\r\n { hash: \u0022a\u0022, amount: 1 },\r\n undefined, // empty data\r\n { hash: \u0022e\u0022, amount: 3 },\r\n { hash: \u0022c\u0022, amount: 1 },\r\n];\r\n\r\nfunction deferPromise(time) {\r\n new Promise((resolve) =\u003E setTimeout(resolve, time));\r\n}","TestCases":[{"Name":"optimize 1","Code":"async function main() {\r\n const arr = [];\r\n const mapHashByIndex = {};\r\n let countBreaker = 0;\r\n await new Promise((resolve) =\u003E {\r\n input.forEach((item, index) =\u003E {\r\n console.log(typeof item, item);\r\n // break if empty data\r\n if (typeof item === \u0022undefined\u0022) {\r\n countBreaker \u002B= 1;\r\n if (index === input.length - 1) resolve();\r\n return;\r\n }\r\n // hash not exists in arr\r\n if (typeof mapHashByIndex[item.hash] === \u0022undefined\u0022) {\r\n // insert to position\r\n arr[index - countBreaker] = item;\r\n mapHashByIndex[item.hash] = index - countBreaker;\r\n console.log(\u0022not found in mapHashByIndex ==\u003E insert to \u0022 \u002B (index - countBreaker));\r\n } else {\r\n // this index not is first index of hash ?\r\n if (mapHashByIndex[item.hash] \u003E index) {\r\n // insert to position = index - countBreaker\r\n arr.splice(index - countBreaker, 0, {\r\n ...arr[mapHashByIndex[item.hash]],\r\n amount: arr[mapHashByIndex[item.hash]].amount \u002B item.amount,\r\n });\r\n // remove position\r\n arr.splice(mapHashByIndex[item.hash], 1);\r\n // incr countBreaker\r\n countBreaker \u002B= 1;\r\n console.log(\r\n \u0022found in mapHashByIndex, repalce index from \u0022 \u002B mapHashByIndex[item.hash] \u002B \u0022 to \u0022 \u002B (index - countBreaker)\r\n );\r\n // update new map\r\n mapHashByIndex[item.hash] = index - countBreaker;\r\n } else {\r\n arr[mapHashByIndex[item.hash]].amount \u002B= item.amount;\r\n console.log(\u0022found in mapHashByIndex, update amount of \u0022 \u002B mapHashByIndex[item.hash]);\r\n // incr countBreaker\r\n countBreaker \u002B= 1;\r\n }\r\n }\r\n if (index === input.length - 1) resolve();\r\n });\r\n });\r\n\r\n console.log(arr);\r\n}\r\n\r\nmain();\r\n","IsDeferred":false},{"Name":"optimize 2","Code":"async function main() {\r\n const arr = [];\r\n const mapHashByIndex = {};\r\n let countBreaker = 0;\r\n await new Promise((resolve) =\u003E {\r\n input.forEach((item, index) =\u003E {\r\n console.log(typeof item, item);\r\n // break if empty data\r\n if (typeof item === \u0022undefined\u0022) {\r\n countBreaker \u002B= 1;\r\n if (index === input.length - 1) resolve();\r\n return;\r\n }\r\n // hash not exists in arr\r\n if (typeof mapHashByIndex[item.hash] === \u0022undefined\u0022) {\r\n // insert to position\r\n arr[index - countBreaker] = item;\r\n mapHashByIndex[item.hash] = index - countBreaker;\r\n console.log(\u0022not found in mapHashByIndex ==\u003E insert to \u0022 \u002B (index - countBreaker));\r\n } else {\r\n // this index not is first index of hash ?\r\n if (mapHashByIndex[item.hash] \u003E index) {\r\n // incr countBreaker\r\n countBreaker \u002B= 1;\r\n // insert to position = index - countBreaker\r\n arr = arr.slice(0, index - countBreaker)\r\n \tconcat([{\r\n ...arr[mapHashByIndex[item.hash]],\r\n amount: arr[mapHashByIndex[item.hash]].amount \u002B item.amount,\r\n }])\r\n .concat(arr.slice(index - countBreaker, mapHashByIndex[item.hash]))\r\n .concat(arr.slice(mapHashByIndex[item.hash], arr.length));\r\n console.log(\r\n \u0022found in mapHashByIndex, repalce index from \u0022 \u002B mapHashByIndex[item.hash] \u002B \u0022 to \u0022 \u002B (index - countBreaker)\r\n );\r\n // update new map\r\n mapHashByIndex[item.hash] = index - countBreaker;\r\n } else {\r\n arr[mapHashByIndex[item.hash]].amount \u002B= item.amount;\r\n console.log(\u0022found in mapHashByIndex, update amount of \u0022 \u002B mapHashByIndex[item.hash]);\r\n // incr countBreaker\r\n countBreaker \u002B= 1;\r\n }\r\n }\r\n if (index === input.length - 1) resolve();\r\n });\r\n });\r\n\r\n console.log(arr);\r\n}\r\n\r\nmain();\r\n","IsDeferred":false}]}