diff --git a/src/0080/index.test.ts b/src/0080/index.test.ts index e6d5ef5..9800d68 100644 --- a/src/0080/index.test.ts +++ b/src/0080/index.test.ts @@ -1,4 +1,4 @@ -import { merge } from '.'; +import { removeDuplicates } from '.'; describe('80. Remove Duplicates from Sorted Array II', () => { interface TestCase { @@ -33,7 +33,7 @@ describe('80. Remove Duplicates from Sorted Array II', () => { test.each(testCases)( 'Case %#', ({ input: { nums }, output: { n, expected } }) => { - const result = merge(nums); + const result = removeDuplicates(nums); expect(result).toBe(n); for (let i = 0; i < result; i++) { expect(nums[i]).toBe(expected[i]); diff --git a/src/0080/index.ts b/src/0080/index.ts index 6c90b4b..0c06f3d 100644 --- a/src/0080/index.ts +++ b/src/0080/index.ts @@ -1,3 +1,11 @@ -function removeDuplicates(nums: number[]): number {} +function removeDuplicates(nums: number[]): number { + let i = 0; + for (const n of nums) { + if (i < 2 || n > nums[i - 2]) { + nums[i++] = n; + } + } + return i; +} export { removeDuplicates };