Hone logo
Hone
Problems

Remove Duplicates from Sorted List

This challenge focuses on efficiently removing duplicate elements from a sorted list while maintaining the original order of the unique elements. This is a common problem in data processing and database management where ensuring data integrity and minimizing storage space are crucial. Your task is to develop a solution that achieves this efficiently.

Problem Description

Given a sorted list of integers, your goal is to remove any duplicate values in-place, meaning you should modify the original list directly. The function should return the new length of the list after removing duplicates. The relative order of the unique elements must be preserved. You are not allowed to allocate extra space (other than constant space for pointers/variables).

Key Requirements:

  • In-place modification: The original list must be modified directly. No new list should be created.
  • Sorted input: The input list is guaranteed to be sorted in ascending order.
  • Unique elements: The output list should contain only unique elements, preserving their original order.
  • Return new length: The function must return the new length of the list after removing duplicates.

Expected Behavior:

The function should iterate through the sorted list, comparing each element to the next. If an element is a duplicate of the previous element, it should be overwritten with the next unique element found further down the list. The process continues until the end of the list is reached. The function then returns the new length of the modified list.

Edge Cases to Consider:

  • Empty list: The list might be empty.
  • List with all duplicates: The list might contain only duplicate elements.
  • List with no duplicates: The list might contain only unique elements.
  • List with a single element: The list might contain only one element.

Examples

Example 1:

Input: [1, 1, 2]
Output: 2
Explanation: The unique elements are [1, 2]. The new length of the list is 2. The list will be modified to [1, 2, _] (where _ represents unused space).

Example 2:

Input: [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
Output: 5
Explanation: The unique elements are [0, 1, 2, 3, 4]. The new length of the list is 5. The list will be modified to [0, 1, 2, 3, 4, _, _, _, _, _] (where _ represents unused space).

Example 3:

Input: []
Output: 0
Explanation: The list is empty, so the new length is 0.

Constraints

  • The input list will contain integers.
  • The list will be sorted in ascending order.
  • The list can contain between 0 and 10000 elements.
  • The space complexity must be O(1) (constant space).
  • The time complexity should be as efficient as possible, ideally O(n), where n is the length of the list.

Notes

Consider using two pointers: one to iterate through the list and another to keep track of the index where the next unique element should be placed. Think about how to efficiently overwrite duplicate elements in-place. Remember that the problem explicitly requires an in-place solution, so avoid creating new lists.

Loading editor...
plaintext