DEV Community

Cover image for LeetCode Challenge: 26. Remove Duplicates from Sorted Array - JavaScript Solution πŸš€
Rahul Kumar Barnwal
Rahul Kumar Barnwal

Posted on β€’ Edited on

1 1 1 1 1

LeetCode Challenge: 26. Remove Duplicates from Sorted Array - JavaScript Solution πŸš€

Top Interview 150

When working with sorted arrays, one common interview problem is removing duplicates in-place while maintaining the relative order of elements. Let’s break down LeetCode 26: Remove Duplicates from Sorted Array and walk through an efficient solution in JavaScript.


πŸš€ Problem Description

Given an integer array nums sorted in non-decreasing order, remove duplicates in-place so that each unique element appears only once. Return the count of unique elements (k) and modify the array so the first k elements contain the unique values.
The rest of the array doesn't matter.


πŸ’‘ Examples

Example 1

Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]
Enter fullscreen mode Exit fullscreen mode

Example 2

Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
Enter fullscreen mode Exit fullscreen mode

🧠 Key Insights

  • Sorted input: Since the array is sorted, duplicates will always appear consecutively.
  • Two-pointer technique: Use two pointers to traverse and overwrite the array while identifying unique elements.

πŸ† JavaScript Solution: Two-Pointer Approach

Here’s the implementation:

var removeDuplicates = function(nums) {
    if (nums.length === 0) return 0; // Handle edge case

    let k = 1; // Pointer for the next unique element position

    for (let i = 1; i < nums.length; i++) {
        if (nums[i] !== nums[i - 1]) {
            nums[k] = nums[i];
            k++;
        }
    }

    return k; // Count of unique elements
};
Enter fullscreen mode Exit fullscreen mode

πŸ” How It Works

  1. Initialize pointers:
    • Start k at 1 to represent the position of the next unique element.
  2. Iterate through the array:
    • If nums[i] is different from the previous element, it’s unique. Copy it to nums[k] and increment k.
  3. Return k:
    • This is the count of unique elements. The first k elements in nums now hold these values.

πŸ”‘ Complexity Analysis

  • > Time Complexity: O(n), where n is the length of the array. We traverse the array once.
  • > Space Complexity: O(1), since no extra space is used.

πŸ“‹ Dry Run

Input: nums = [0,0,1,1,1,2,2,3,3,4]
Dry Run

Output: k = 5, nums = [0,1,2,3,4,_,_,_,_,_]


✨ Pro Tips for Interviews

  1. Ask clarifying questions:
    • Confirm if the order of elements must be preserved.
  2. Consider edge cases:
    • Empty array.
    • Array with all identical elements.
  3. Explain the two-pointer logic clearly:
    • It’s simple but highly effective for problems requiring in-place modifications.

πŸ“š Learn More

Check out the full explanation and code walkthrough on Dev.to:
πŸ‘‰ Remove Element - JavaScript Solution

How would you approach this problem? Let me know in the comments! πŸš€

JavaScript #LeetCode #CodingInterview #ProblemSolving

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (1)

Collapse
 
rahulgithubweb profile image
Rahul Kumar Barnwal β€’

Follow Me on GitHub πŸš€

If you found this solution helpful, check out more of my projects and solutions on my GitHub profile.

Don't forget to follow for more updates!

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More