Leetcode | Return result is always empty

Viewed 154

I'm going over some array problems on Leetcode and when I tried to submit my answer I got an empty result as my answer. I'm not sure why it's happing. I tried to run this function in Chrome DevTools and it works as expected. Any hints would be appreciated.

enter image description here

/**
 * @param {number[]} nums
 * @return {number}
 */
function removeDuplicates(nums) {
    return nums.filter((n, index) => nums[index] != nums[++index])
}

Link to the problem: https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/727/

2 Answers

Ok this is strange because I went and even logged to the console.log my correct answer but the return is.. very strange.. so far it seems that this is not your fault

I even tried making the return be a string and it still shows this empty array.. it's strange FOR REAL

enter image description here

Is nums unsorted? If so, you'd need to do something like this:

function removeDuplicates(nums) {
    const set = new Set();

    return nums.filter(n => {
        if (!set.has(n)) {
            set.add(n);
            return true;
        }

        return false;
    });
}

Otherwise, if sorted, your solution seems right.

Related