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

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

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!

Sentry image

See why 4M developers consider Sentry, โ€œnot bad.โ€

Fixing code doesnโ€™t have to be the worst part of your day. Learn how Sentry can help.

Learn more