imashutosh51

Remove Duplicates from Sorted Array II

Aug 16th, 2026
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1. class Solution:
  2.     def removeDuplicates(self, nums: List[int]) -> int:
  3.         k = 0
  4.         count = 0
  5.  
  6.         for i in range(len(nums)):
  7.  
  8.             # New number, reset count
  9.             if i == 0 or nums[i] != nums[i - 1]:
  10.                 count = 1
  11.             else:
  12.                 count += 1
  13.  
  14.             # Keep at most 2 occurrences
  15.             if count <= 2:
  16.                 nums[k] = nums[i]
  17.                 k += 1
  18.  
  19.         return k
Advertisement
Add Comment
Please, Sign In to add comment