Click here to Skip to main content
15,888,313 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Leet code (26th problem)
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.

ERROR:
ERROR: AddressSanitizer: heap-buffer-overflow on address


What I have tried:

C++
int removeDuplicates(int* nums, int numsSize){
     int i=1,k=1;
    if(numsSize==1)
        return numsSize;
    while(k<numsSize)
    {if(nums[k]==nums[k-1])
        k++;
     else
         nums[i++]=nums[k++];}
    return i;
}
Posted
Updated 14-Jun-21 20:34pm
Comments
Richard MacCutchan 15-Jun-21 4:33am    
I tried that code and it worked fine.
_-_-_-me 15-Jun-21 6:57am    
Thank you !
I tried that in online compiler , it worked fine.
But when I tried in the compiler that was provided in the question of leet code ,
I am getting this error.
Richard MacCutchan 15-Jun-21 7:22am    
I don't think anyone here can help with that.
Richard MacCutchan 15-Jun-21 7:36am    
I just tried it on the leet website and it works fine. Is there some other information that you have not shown us?
_-_-_-me 15-Jun-21 7:55am    
Thank you very much !
Thanks for trying that in the leet code website.
No,Exactly I copied and pasted it here. There is no other information that
I have note shown you .
I completely pasted my answer.
It is 26th question.


In the question part this is remaining I have not shown :

Custom Judge:

The judge will test your solution with the following code:

int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length

int k = removeDuplicates(nums); // Calls your implementation

assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}

If all assertions pass, then your solution will be accepted.

1 solution

Probably, it's this:
C
while(k<numsSize)
{
 ...
     nums[i++]=nums[k++];}
When k == numsize - 1 then k++ is outside the memory allocated.

But that's just a guess, I don't have your data to check against.
So, it's going to be up to you.

Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

And do yourself a favour: indent your code properly.
That's not easy to read, and shows a total lack of care in producing it ...
 
Share this answer
 
Comments
_-_-_-me 15-Jun-21 1:29am    
Thank you very much!
You told to learn the debugging a while ago.
I started using debugger when you told the first time to learn the skill
debugging.
Because of that I was able to find the errors in code .
Thanks for saying that!
In this,
When I debugged the code in the online compiler(onlinegdb.com),
Even though the k value of k is increased to numsSize , but it is not accessing
the array memmory as while loop terminates.
and I am not getting error in online compiler.

When I the submit the answer to the question through the compiler in leetcode , I am getting this error.
In that compiler , I was unable to debug because debug option is not accessible to
me.
CPallini 15-Jun-21 1:55am    
5.However, nums[k++] is a safe array access, AFAIK.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900