Remove Duplicates from Sorted Array
You can solve this problem using a twopointer approach. Since the array is sorted, duplicates will be adjacent to each other. Here’s a stepbystep explanation of how you can achieve this:
 Create two pointers,
i
andj
, both starting at 0.  Traverse through the array using pointer
j
.  If
nums[j]
is different fromnums[i]
, copy the value ofnums[j]
tonums[i + 1]
, and incrementi
.  Continue this process until
j
reaches the end of the array.  The length of the unique elements will be
i + 1
.
Here’s the code that implements this logic:


This code will remove duplicates from the array nums
inplace, and return the number of unique elements. The time complexity of this solution is ( O(n) ), where ( n ) is the length of the array, and the space complexity is ( O(1) ) as no additional space is required.
Identifying Problem Isomorphism
“Remove Duplicates from Sorted Array” can be approximately mapped to “Remove Element”.
Reasoning:
These two problems are closely related as they both require inplace manipulation of an array.
“Remove Duplicates from Sorted Array” asks you to remove all duplicate elements from a sorted array and return the new length.
“Remove Element” requires you to remove all instances of a specified value from the array and return the new length.
While the constraints differ slightly, the challenge in both problems is to traverse an array while adjusting its contents and keeping track of a count (the new length). As such, many of the same techniques apply.
In terms of complexity, both problems are roughly equivalent as they both involve simple array traversal and manipulation. No complex data structures or algorithms are necessary.


Problem Classification
The problem is about Array Manipulation and Duplicate Removal.
Array Manipulation: The problem involves altering the structure of an existing array (in this case, moving duplicate values around), which falls under the broad category of array manipulation.
Duplicate Removal: The problem specifically asks to remove duplicates from the list, which is a common task in data processing and falls under the specific problem type of duplicate removal.
The problem is about Inplace Algorithm Design, since you’re required to solve the problem using a constant amount of space i.e., not using additional space proportional to the size of the input. However, this might be considered as a constraint or a subproblem, rather than a classification of the problem domain itself.
Language Agnostic Coding Drills
Understanding the list structure and accessing elements: In this problem, we need to understand how to work with a list of numbers. Specifically, we need to know how to access individual elements in the list using indices. This is shown in the
for
loop where we iterate through the list starting from the second element (index 1).Understanding variables and assignment: This problem requires creating a counter variable (
j
), starting at 0, that keeps track of the position of unique numbers in the list. This is important in the process of overwriting duplicates.Conditional statements and comparisons: The
if
statement is used to compare whether the current numbernums[i]
is different from the last unique numbernums[j]
. If it is different, it means this number is a unique number that we have not encountered before.Updating variables based on conditions: When a new unique number is found, we increment
j
by 1 and replace the number at indexj
with the new unique number. This is the key step that removes duplicates from the list inplace.Understanding the return statement: The return statement gives us the length of the list after duplicates have been removed, which is
j + 1
becausej
is the index of the last unique number in the list, and indices are 0based.
So in increasing level of difficulty, the drills could be:
 Drill on accessing elements in a list using indices and iterating over a list.
 Drill on using variables and updating them based on conditions.
 Drill on using if statements and doing comparisons.
 Drill on manipulating list elements based on conditions.
 Drill on forming and returning the final result.
In general, the approach is to maintain a pointer j
that always points to the last unique number in the list. We iterate through the list, and whenever we find a number that is not equal to nums[j]
, we increment j
and overwrite nums[j]
with the new number. This approach ensures that all duplicates are overwritten, and the first j + 1
elements of the list are unique.
Targeted Drills in Python
 Drill 1  Accessing elements in a list using indices and iterating over a list:


 Drill 2  Using variables and updating them based on conditions:


 Drill 3  Using if statements and doing comparisons:


 Drill 4  Manipulating list elements based on conditions:


 Drill 5  Forming and returning the final result:


Now to integrate all these drills into the final solution:


This will help to solidify the understanding of these concepts and provide a stepbystep approach to the final problem.