Home:ALL Converter>Remove duplicates from a sorted array - Leet Code 26

Remove duplicates from a sorted array - Leet Code 26

Ask Time:2022-01-02T23:19:34         Author:N8BIZ

Json Formatter

I'm working through a LeetCode challenge 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.

I'm not sure why my answer isn't being accepted. I believe I'm misinterpreting something simple.

The issue:

My solution only returns an empty array even though I appear to have the correct answer for the baseline test. Is there something that I'm overlooking on the implementation in regard to the rules of the challenge? splice edits elements in place.... so I thought that would be fine.

Any suggestions would be appreciated.

var removeDuplicates = function(nums) {
    const map = new Map();
    let count = 0;
    nums.forEach((item, i) => {
        if (map.get(item) === undefined){
            map.set(item, i);
        } else if (map.get(item) !== undefined) {
            nums.splice(i, 1);
            nums.push('_');
            count ++;
        }
    });
    return count;
};

I have also posted this as a question on LeetCode's discussion section.

Author:N8BIZ,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/70557174/remove-duplicates-from-a-sorted-array-leet-code-26
trincot :

You shouldn't splice out items from an array that you are iterating, as such iteration is based on an incrementing index, and the deletion will cause array values to shift to the left. So this double effect will make that you skip array values.\nMoreover, you should avoid splicing all together as it represents a O(n) time complexity.\nAlso, the assignment says "Do not allocate extra space ... You must do this ... with O(1) extra memory.", so collecting values in a Map is not what you are supposed to do. As the input array is sorted you really don't need this map either.\nInstead use two indices: one that will be used for reading a value, and one where a value will be written. The first one will run ahead of the latter when there are duplicates.\nvar removeDuplicates = function(nums) {\n if (nums.length == 0) return 0;\n let k = 0;\n for (let value of nums) {\n if (value != nums[k]) {\n nums[++k] = value; \n }\n }\n return k + 1;\n};\n\nNote that it is not necessary to modify the length of the array, as the code challenge says:\n\n...the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.\n",
2022-01-02T16:20:37
yy