3

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/

6
  • What problem are you trying to solve? Commented Jul 14, 2021 at 21:13
  • Here's the link: leetcode.com/explore/interview/card/… Commented Jul 14, 2021 at 21:14
  • 2
    the code seems to work on my end.. either way ur code wouldn't solve all problems with that logic ;-; Commented Jul 14, 2021 at 21:28
  • bruh i think u shud take it up with leetcode cuz this is super strange Commented Jul 14, 2021 at 21:41
  • Did anything come of this? I having the same issue on leetcode. Commented Feb 15, 2024 at 3:32

4 Answers 4

1

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

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

1 Comment

Indeed, it's super strange. Thanks for confirming it. I already reported this issue to the Leetcode helpdesk. I hope, they'll get back to me asap.
0

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.

3 Comments

the error isn't his code.. it's that the return is an empty array no matter what you return ;-;
well according to the code question.. it asks to remove the duplicates in-place so sorting the answer array isn't necessary.. however if u just clicked the link and read at the very beginning it says Given an integer array nums sorted in non-decreasing order
Link didn’t work for me, so I didn’t see that. That’s why I asked if nums was unsorted or not.
0

The issue personally was with the phrasing of this problem. You're expected to mutate the nums array which goes into the input. Whereas your return only expects the number of unique elements k.

It uses that number k to iterate through the mutate nums array.

I probably never paid to much attention to the Custom Judge portion of that question to figure it out.

Comments

0

It worked for me the idea is to not create a new nums array instead reuse the same array to make changes in it and return the count of unique values rather than the nums array itself here is a code that got accepted do note this is not the most optimised approach for the same in terms of space

var removeDuplicates = function(nums) {
    let cnt = 1;
    let uniqueIdx = []
    if(nums.length==0) return 0;

    for(let i=0 ; i<nums.length-1; i++) {
        if(nums[i] != nums[i+1]) {
            cnt++;
            uniqueIdx.push(nums[i])
            continue;
        }
    
    }
    uniqueIdx.push(nums[nums.length-1])

    for(let i=0; i<uniqueIdx.length; i++) {
        nums[i] = uniqueIdx[i]
    }

    return cnt 
};

working sample for the same

enter image description here

Comments

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.