Files
second-brain/practice/LeetCode/Solutions/0026-remove-duplicates-from-sorted-array.py
T
kiskaadee ff59aa44af
Brain Structural Validation / Validate Markdown Structure & Links (push) Failing after 4s
refactor(practice): modernize type annotations and fix ruff linting in LeetCode solutions
- Update typing.List to built-in list[T]
- Annotate SYMBOLS dictionary with ClassVar in 0013-roman-to-integer
- Combine nested if conditions in 0028 and 0058 solutions
2026-09-10 08:55:56 -05:00

45 lines
1.1 KiB
Python

class Solution:
def removeDuplicates(self, nums: list[int]) -> int:
if not nums:
return 0
w = 1
for i in range(1, len(nums)):
if nums[i] != nums[i-1]:
nums[w] = nums[i]
w+=1
return w
if __name__ == "__main__":
sol = Solution()
test_cases = [
# input, expected_k, expected_prefix
([1, 1, 2], 2, [1, 2]),
([0, 0, 1, 1, 1, 2, 2, 3, 3, 4], 5, [0, 1, 2, 3, 4]),
([1, 2, 3, 4, 5], 5, [1, 2, 3, 4, 5]),
([2, 2, 2, 2], 1, [2]),
([], 0, []),
([1], 1, [1]),
]
for i, (nums, expected_k, expected_prefix) in enumerate(test_cases, start=1):
nums = nums[:]
k = sol.removeDuplicates(nums)
passed = (
k == expected_k and nums[:k] == expected_prefix
)
if passed:
print(f"Test {i} PASSED")
else:
print(f"Test {i} FAILED")
print(f" expected k: {expected_k}, got k={k} ")
print(f" expected prefix: {expected_prefix}, got {nums[:k]}")
print(f" array state: {nums}")