mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-09-08 04:05:54 -05:00
Fix stack overflow in diff() for large element counts
This commit is contained in:
@@ -36,6 +36,12 @@ describe("diff", () => {
|
||||
const result = diff(arr1, arr2);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it("should not overflow the stack for very large counts", () => {
|
||||
const arr2 = new Array(200_000).fill(1);
|
||||
const result = diff([], arr2);
|
||||
expect(result).toHaveLength(200_000);
|
||||
});
|
||||
});
|
||||
|
||||
describe("mostPopularArrayElement", () => {
|
||||
|
||||
@@ -53,7 +53,9 @@ export function diff<T extends string | number>(arr1: T[], arr2: T[]): T[] {
|
||||
const result: T[] = [];
|
||||
|
||||
for (const [element, count] of diff) {
|
||||
result.push(...new Array(count).fill(element));
|
||||
for (let i = 0; i < count; i++) {
|
||||
result.push(element);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user