0

LeetCode problem 26 - Remove Duplicates From Sorted Array

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

Return k after placing the final result in the first k slots of nums.

Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

var removeDuplicates = function (nums) {
    // Iterating the full array
    for (let i = 0; i < nums.length; i++) {
        // Checking for the repeating number
        if (nums[i] === nums[i + 1]) {
            // Removing the element which is repeating
            nums = nums.slice(0, i + 1).concat(nums.slice(i + 2));
            // Resetting the index after removing the element
            i--;
        }
    }
    console.log(nums);
    return nums.length;
};

console.log(removeDuplicates([0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 4]));

This is the output of the above JS code on Visual Studio Code.

I am not able to submit this code to LeetCode. The expected output is the edited nums array, whereas my output doesn't match. How do I resolve this issue?

3
  • 1
    You're not following the directions. They want the ending array to be the exact same size as the starting array. You're just supposed to be sliding elements down. Commented Jan 1, 2022 at 6:05
  • @FrankYellin it is mentioned in the problem statement that only the first k elements of the array matter. The elements that succeed the first k elements are not analysed. Commented Jan 1, 2022 at 15:49
  • The instructions are pretty clear that they don't want you slicing. "It does not matter what you leave" implies that you do leave something. I agree they didn't word it well. Commented Jan 1, 2022 at 20:04

1 Answer 1

0

Your solution is too complicated. To remove duplicates from an array, just do:

const removeDuplicates = (arr) => [...new Set(arr)]

console.log(removeDuplicates([0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 4]));

Sign up to request clarification or add additional context in comments.

5 Comments

I have tried the Set version, but it does not work. The output is rejected.
I have provided a working exemple just above. Please provide the usecase (input, output) where it doesn't work.
Link to problem I checked your code on the leetcode website, and it doesn't work.
Leetcode is an awful platform, many devs rant against it. My code works 100% fine. Just run it in node or online with codepen, codesandbox, etc. This function returns the expected output. Also this code answers correctly to your question, SO is not made to solve Leetcode issues.
The instructions are clear that the solution has to be O(1) memory. This solution fails that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.