Posts

Showing posts from May, 2024

26. Remove Duplicates from Sorted Array (Easy)

Image
Remove Duplicates from Sorted Array is an Easy level problem. Given an array of integers sorted in non-decreasing (aka, increasing) order, remove all the duplicate values IN PLACE and return the number of unique values in the list. My first solution to this was:  c lass Solution : def removeDuplicates ( self , nums : list[ int ]) -> int : dupeCount = 0 origLength = len (nums) i = 0 while (i < len (nums) - 1 ): thisElem = nums[i] nextElem = nums[i+ 1 ] while thisElem == nextElem: dupeCount += 1 if (i< len (nums)- 2 ): nums[i+ 1 :] = nums[i+ 2 :] else : nums[i+ 1 :] = [] if i+ 1 < len (nums): nextElem = nums[i+ 1 ] else : nextElem = None i += 1 return origLength - dupeCount That solution worked, but it was super slow...