0

I am working on the Leetcode in JS,

Question 26th (Remove Duplicates from Sorted Array)

I am trying with Set (ES6), but it is not working on the Leetcode page (for direct submitting), however it's working in the console.

Besides, I also found the old answers have already listed Set as a solution. Here's old post!

From the old post, the author said:

ES6 provides the Set object, which makes things a whole lot easier

// code from the old post

function uniq(a) {
   return Array.from(new Set(a));
}
or

let uniq = a => [...new Set(a)];

Here are my codes with Set:

//this is my code with Set

var removeDuplicates = function(nums) {
    let set = new Set(nums);
    let setArr = [...set];
    return setArr;
};


And here is what is displayed after code run on Leetcode page,the output is different from the expected:

On Leetcode Page

And here is the display on a webpage's console
Console

Could anyone help me to understand what are the reasons behind? Or I just misunderstood the question? Thank you!

  • 2
    Leetcode might be running some kind of old/broken engine/transpiler. Does `Array.from(set)` instead of `[...set]` work, maybe? – Ry- Jul 23 '19 at 20:10
  • btw, i would not use `set` as variable name, because this is a word with a special meaning and depends on the context. – Nina Scholz Jul 23 '19 at 20:12
  • I tried with Array.from(set).length, still not working, same result as [...set].length – puddlejumper26 Jul 23 '19 at 21:54
  • Thanks, u r right, I should not use set as a variable name, it causes some confusions – puddlejumper26 Jul 23 '19 at 21:56
  • @Ry- Leetcode works fine ^^, image shows leetcode giving success run (but wrong answer) instead of error – juvian Jul 23 '19 at 21:57
  • @juvian: I was thinking a broken implementation of array spread that required an array rather than iterable value and looped up to the non-existent `set.length` might be the issue, because the output it showed (an empty array) makes no sense at all. – Ry- Jul 23 '19 at 22:48
  • @Ry- they expect you to return a number and they print the list passed up to that number. I would guess returning an array (not integer) ends up being considered as a 0 instead of giving some kind of error. Then no elements get printed and you get an empty array. If it was changed to return setArr.length, it would show something so the code certainly works as far as spread is concerned. – juvian Jul 23 '19 at 23:06

1 Answers1

2

Your function perfectly removes duplicates from an array, but it does not do what the leetcode task asks for:

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

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

Also, the expected return as it says there is a single number, not an array. It states that you need to return the length of the new array (number of unique elements).

Community
  • 1
  • 1
juvian
  • 15,875
  • 2
  • 37
  • 38
  • when I use [...new Set(num)].length, it's still not working, and if I run this code under console, it shows correctly – puddlejumper26 Jul 23 '19 at 21:50
  • @puddlejumper26 In that case length will be correct but you are still not completing the task: remove the duplicates in-place, which means modify the array given to you so that the first x elements correspond to the unique elements it has in sorted order. Also it says do not allocate extra space for another array, so using new Set(nums) is also forbidden. – juvian Jul 23 '19 at 21:54
  • thanks, now I am catching up. I misunderstood the 'do not allocate extra space for another array' part. – puddlejumper26 Jul 23 '19 at 22:00