Skip to content

JS solution for 0080 Remove Duplicates from Sorted Array II added #2568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

majhar-nayem
Copy link

Important

Please make sure the file name is lowercase and a duplicate file does not already exist before merging.

@majhar-nayem majhar-nayem changed the title JS solution for Remove Duplicates from Sorted Array II added JS solution for 0080 Remove Duplicates from Sorted Array II added Jun 11, 2023
@@ -0,0 +1,22 @@
/**
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Add Time O() | Space O() complexity

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time O(n) || Space O(1)

added.

@majhar-nayem majhar-nayem requested a review from aakhtar3 July 1, 2023 03:47
@@ -1,22 +1,26 @@
/**
/*
Copy link
Collaborator

@aakhtar3 aakhtar3 Jul 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original code was following the common 2-pointer algorithm approach. This change has moved from that pattern and made it more difficult to retain the 2-pointer approach.

Here’s a modified version for the 2-pointer algorithm.

/**
 * 2-pointer
 * Time O(N) | Space O(1)
 * https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii
 * @param {number[]} nums
 * @return {number}
 */
var removeDuplicates = (nums) => {
    const isEdgeCase = (nums.length < 2)
    if (isEdgeCase) return nums.length;

    let [ left, right ] = [ 2, 2 ];

    while (right < nums.length) {
        const isEqual = (nums[left - 2] === nums[right]);
        if (!isEqual) {
            nums[left] = nums[right];
            left += 1;
        }

        right += 1;
    }

    return left;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants