31 lines
1.2 KiB
Python
31 lines
1.2 KiB
Python
def removeDuplicates(nums):
|
|
"""
|
|
Remove duplicates from a list of integers and return the length of the resulting list.
|
|
|
|
Args:
|
|
nums (List[int]): A list of integers
|
|
|
|
Returns:
|
|
int: The length of the resulting list after removing duplicates
|
|
|
|
Example:
|
|
>>> removeDuplicates([1,1,1,2,2,3])
|
|
3
|
|
"""
|
|
k = len(nums) # Initialize k with the length of the input list nums
|
|
|
|
for i in range(len(nums)):
|
|
tempNum = 0
|
|
# Iterate over the elements of nums starting from index i
|
|
for j in range(i+1, len(nums)):
|
|
temp = nums[i]
|
|
if temp == nums[j]: # Check if a duplicate element is found
|
|
tempNum += 1
|
|
if tempNum > 1 and nums[j] != float('inf'):
|
|
# If it's the second occurrence and the element is not already marked as 'inf',
|
|
# mark it as 'inf' to indicate it as a duplicate and decrement k by 1
|
|
nums[j] = float('inf')
|
|
k -= 1
|
|
|
|
nums.sort() # Sort the list in ascending order
|
|
return k # Return the length of the resulting list without duplicates |