Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

Remove Duplicates from Sorted Array Two Pointer in DSA Javascript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Remove Duplicates from Sorted Array Two Pointer
Start with two pointers
↓
Pointer1 at index 0
↓
Pointer2 at index 1
↓
Compare elements at Pointer1 and Pointer2
↓
Move Pointer2 forward
↓
Repeat until Pointer2 reaches end
↓
Return length = Pointer1 + 1
Use two pointers to scan the array. One pointer tracks unique elements, the other scans ahead to find new unique values to copy forward.
Execution Sample
DSA Javascript
function removeDuplicates(nums) {
  let i = 0;
  for (let j = 1; j < nums.length; j++) {
    if (nums[j] !== nums[i]) {
      i++;
      nums[i] = nums[j];
    }
  }
  return i + 1;
}
This code removes duplicates from a sorted array by moving unique elements forward and returns the new length.
Execution Table
StepOperationPointer iPointer jArray StateAction
0Initialize pointers01[1, 1, 2, 2, 3]Start with i=0, j=1
1Compare nums[j] and nums[i]01[1, 1, 2, 2, 3]nums[1]=1 equals nums[0]=1, no change, move j
2Move j forward02[1, 1, 2, 2, 3]j=2
3Compare nums[j] and nums[i]02[1, 1, 2, 2, 3]nums[2]=2 not equal nums[0]=1, increment i and copy
4Increment i and copy nums[j]12[1, 2, 2, 2, 3]nums[1]=2 copied from nums[2]
5Move j forward13[1, 2, 2, 2, 3]j=3
6Compare nums[j] and nums[i]13[1, 2, 2, 2, 3]nums[3]=2 equals nums[1]=2, no change, move j
7Move j forward14[1, 2, 2, 2, 3]j=4
8Compare nums[j] and nums[i]14[1, 2, 2, 2, 3]nums[4]=3 not equal nums[1]=2, increment i and copy
9Increment i and copy nums[j]24[1, 2, 3, 2, 3]nums[2]=3 copied from nums[4]
10Move j forward25[1, 2, 3, 2, 3]j=5 (end)
11End loop25[1, 2, 3, 2, 3]Return length = i+1 = 3
💡 j reaches array length 5, loop ends
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7After Step 9Final
i001122
j123445
nums[1,1,2,2,3][1,1,2,2,3][1,2,2,2,3][1,2,2,2,3][1,2,3,2,3][1,2,3,2,3]
Key Moments - 3 Insights
Why do we increment pointer i only when we find a new unique element?
Pointer i marks the position of last unique element. We only move it forward to store a new unique value, as shown in steps 3 and 8 in the execution_table.
Why don't we move pointer i when nums[j] equals nums[i]?
Because the element is a duplicate, we skip copying it and only move pointer j forward to check the next element, as seen in steps 1 and 6.
What does the returned length represent?
It is i + 1 because i is zero-based index of last unique element. So length counts all unique elements, as in step 11.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of pointer i?
A1
B0
C2
D3
💡 Hint
Check the 'Pointer i' column at step 4 in execution_table
At which step does the array first change its content?
AStep 1
BStep 4
CStep 6
DStep 9
💡 Hint
Look at the 'Array State' column and see when the array changes from original
If the input array had no duplicates, how would pointer i change?
AIt would stay at 0
BIt would increment with pointer j each time
CIt would increment only once
DIt would decrement
💡 Hint
Pointer i moves forward only when a new unique element is found, see variable_tracker for i increments
Concept Snapshot
Remove duplicates from sorted array using two pointers:
- Pointer i tracks last unique element index
- Pointer j scans ahead
- When nums[j] != nums[i], increment i and copy nums[j]
- Continue until j reaches end
- Return i + 1 as new length
Full Transcript
This concept uses two pointers to remove duplicates from a sorted array in place. Pointer i marks the position of the last unique element found. Pointer j scans through the array. When nums[j] is different from nums[i], we move i forward and copy nums[j] to nums[i]. This way, all unique elements are moved to the front. The process continues until pointer j reaches the end of the array. The function returns i + 1, which is the count of unique elements. The execution table shows each step, pointer positions, array changes, and actions taken. Variable tracker shows how pointers and array change over time. Key moments clarify why pointer i moves only on unique elements and what the returned length means.

Practice

(1/5)
1. What is the main purpose of using the two-pointer technique in removing duplicates from a sorted array?
easy
A. To reverse the array while removing duplicates
B. To sort the array again after duplicates are removed
C. To count the total number of duplicates in the array
D. To efficiently overwrite duplicates and keep unique elements at the start

Solution

  1. Step 1: Understand the two-pointer technique

    One pointer tracks the position to place the next unique element, the other scans the array.
  2. Step 2: Purpose of the technique

    It overwrites duplicates by placing unique elements at the front, avoiding extra space.
  3. Final Answer:

    To efficiently overwrite duplicates and keep unique elements at the start -> Option D
  4. Quick Check:

    Two-pointer = overwrite duplicates [OK]
Hint: Two pointers: one scans, one writes unique values [OK]
Common Mistakes:
  • Thinking it sorts the array again
  • Confusing counting duplicates with removing them
  • Assuming it reverses the array
2. Which of the following is the correct syntax to initialize two pointers for removing duplicates from a sorted array nums in JavaScript?
easy
A. let i = 1, j = 0;
B. let i = 0, j = 1;
C. let i = 0, j = 0;
D. let i = 1, j = 1;

Solution

  1. Step 1: Identify pointer roles

    Pointer i tracks last unique element index, j scans from next element.
  2. Step 2: Correct initialization

    i starts at 0 (first element), j starts at 1 (second element) to compare.
  3. Final Answer:

    let i = 0, j = 1; -> Option B
  4. Quick Check:

    i=0, j=1 start positions [OK]
Hint: Start i at 0, j at 1 to compare neighbors [OK]
Common Mistakes:
  • Starting both pointers at 0
  • Swapping i and j initialization
  • Starting both pointers at 1
3. What will be the output array after removing duplicates using the two-pointer method on nums = [1, 1, 2, 2, 3]?
medium
A. [1, 2, 3]
B. [1, 1, 2, 3]
C. [1, 2, 2, 3]
D. [1, 3, 2]

Solution

  1. Step 1: Traverse and compare elements

    Start with i=0, j=1. Compare nums[j] with nums[i]. If different, increment i and copy nums[j].
  2. Step 2: Update array with unique elements

    After processing, unique elements at start are [1, 2, 3].
  3. Final Answer:

    [1, 2, 3] -> Option A
  4. Quick Check:

    Unique sorted elements = [1, 2, 3] [OK]
Hint: Unique elements stay at front in sorted order [OK]
Common Mistakes:
  • Leaving duplicates in the output
  • Mixing order of elements
  • Not updating the array in place
4. Consider this code snippet for removing duplicates from a sorted array nums. What is the bug?
let i = 0;
for (let j = 1; j < nums.length; j++) {
  if (nums[j] !== nums[i]) {
    i = j;
    nums[i] = nums[j];
  }
}
return i + 1;
medium
A. Pointer i should be incremented, not assigned j
B. The loop should start from j = 0
C. The comparison should be nums[j] === nums[i]
D. Return statement should be i instead of i + 1

Solution

  1. Step 1: Analyze pointer update

    i should move forward by incrementing to next unique position, not jump to j.
  2. Step 2: Correct pointer increment

    Use i++ to move i forward, then assign nums[i] = nums[j].
  3. Final Answer:

    Pointer i should be incremented, not assigned j -> Option A
  4. Quick Check:

    i++ to move pointer forward [OK]
Hint: Increment i to place unique elements, don't assign j to i [OK]
Common Mistakes:
  • Assigning i = j instead of i++
  • Starting loop at wrong index
  • Wrong comparison operator
5. Given a sorted array nums = [0,0,1,1,1,2,2,3,3,4], what will be the length of the array after removing duplicates using the two-pointer method, and what will the first part of the array look like?
hard
A. Length: 4, Array start: [0, 1, 2, 3]
B. Length: 6, Array start: [0, 1, 1, 2, 3, 4]
C. Length: 5, Array start: [0, 1, 2, 3, 4]
D. Length: 5, Array start: [0, 0, 1, 2, 3]

Solution

  1. Step 1: Apply two-pointer method on nums

    Traverse array, move unique elements forward: 0,1,2,3,4.
  2. Step 2: Count unique elements and array start

    There are 5 unique elements, so length is 5, and array start is [0,1,2,3,4].
  3. Final Answer:

    Length: 5, Array start: [0, 1, 2, 3, 4] -> Option C
  4. Quick Check:

    Unique count = 5, array front = [0,1,2,3,4] [OK]
Hint: Count unique elements, array front holds them [OK]
Common Mistakes:
  • Counting duplicates as unique
  • Not updating array in place
  • Miscounting length after removal
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard