Skip to content

Commit

Permalink
solution: 0080. Remove Duplicates from Sorted Array II
Browse files Browse the repository at this point in the history
  • Loading branch information
flynnpark committed Jan 4, 2024
1 parent d8de2b3 commit a5bcfa9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/0080/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { merge } from '.';
import { removeDuplicates } from '.';

describe('80. Remove Duplicates from Sorted Array II', () => {
interface TestCase {
Expand Down Expand Up @@ -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]);
Expand Down
10 changes: 9 additions & 1 deletion src/0080/index.ts
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit a5bcfa9

Please sign in to comment.