# Remove Duplicates from Sorted Array

• hey for this case........2,3,1,1,2,1,6

• @srikanth007 The array is sorted so that is an invalid test case

• ``````int removeDuplicates(int* nums, int numsSize)
{
int i = 0;
int j = 0;

for(j = 0; j < numsSize ; j ++){
if (j + 1 < numsSize && nums[j] != nums[j + 1]){
nums[i] = nums[j];
i ++;
}
else if(j + 1 == numsSize){
nums[i] = nums[j];
i ++;
}
}

return i;
}
``````

• You said to remove entries from array. nums's length is still 3.

• The description was ambigous and misleading. Your problems states exactly the following:
'Your function should return length = 2 ...'

Well i returned the length, so your validator shouldn't give a damn what i do to the array as long as the output is correct.
I used sort to sort the array first, turn out your array was already sorted the way i wanted it, but you didn't makethis clear in the specification.

• Please revise the test cases on this problem. Here is my code so far; it returns the length and modifies the nums array correctly. However, the test case for [1,1,2] is failing even though i return the proper length of 2 and I set nums to be [1,2]. It keeps stating that my output is [1,null]. For an easy problem this is quite ambiguous; where is that coming from? Isn't my output simply length? That is all I am returning.

``````var removeDuplicates = function(nums) {

var len = nums.length;
var currentComparisonVal = nums[0];

for(var i = 1; i < nums.length; i++){
if(nums[i] == currentComparisonVal){
len--;
nums[i] = null;
}
else{
currentComparisonVal = nums[i];
}
}

nums = nums.filter(function(n){ return n });

return len;
``````

};

• @aruballos The validator actually checks what you do to the array.

You need to return correct length, true. But at the same time they want your array to be [1,2] after you are done with the [1, 1, 2] input.

Here's my solution, in case you might find it helpful.

``````var removeDuplicates = function(nums) {

for (var i = 0; i < nums.length-1; i++) {
while (nums[i] == nums[i+1]) {
nums.splice(i, 1);
}
}

return nums.length;
};
``````

Easier removal of unwanted element, is what it does.

I would avoid assigning null within your array. That really isn't needed. You just want to remove duplicates.

• @SpiritusSancti5 Thanks for following up! Splice is a much cleaner solution than my loop and filter.

What I don't understand is why my nums is failing validation though; the filter call should be correctly configuring the nums array to [1,2] and my console.log calls seem to indicate this is the case.

• Ambiguous description, I too thought that returning the length is enough. but it is not.
more examples would be nice.

• I agree with dynyanada. Terrible description.

• Terrible description.

• @aruballos All you're doing to the given array is overwrite some values with `null`. Your `nums = nums.filter(function(n){ return n });` does nothing to the given array. It just creates another array (which the problem btw explicitly forbids) that is then lost when you return.

Then you return 2, so the judge looks at the first two elements of the given array and sees the `1` and `null` that you left there.

• A particularly poorly worded and poorly described problem. My implementation in Python correctly altered the nums input, and correctly returns the length of nums. I specifically printed my nums array before returning and it showed the correct expected output, yet the Judge kept showing a different output. Very strange.

• @cjgali1 Yeah,I agree with you ! When I saw the expected output,I thought it expected an array but not a length!!!

• Here is my code:

class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return len(list(set(nums)))

why [1,2] when the input is [1,1,2]??????

• (void)removeDuplicatesFromSortedArray:(NSMutableArray *)iArray {
//1,1,2,3,4,4,5,6,7,7,8,11
if (iArray.count == 0) {
return;
}
NSUInteger length = iArray.count - 1;

for (int j = 0; j < length; j++) {
if ([iArray[j] isEqual:iArray[j+1]]) {
[iArray removeObjectAtIndex:j];
length = length - 1;
}
}
NSLog(@"array after removing duplicate elements..%@", iArray);
}

O(n) and O(1)

• I am still a bit confused with this problem.... @9431, I believe the problem is meant to show a strategy for iterating through a list/tree in an array, without necessarily removing the duplicates, just assigning the elements following the given pattern. The sample states that the first step should appear correct and it does not matter what follows as long as you return the correct sum of the list as if duplicates were removed.. Does that seem accurate?

• @9431 Unfortunately that solution won't work because you do not alter the array in place, as the question specifies. In theory, saying something like "nums = set(nums)" would work, but in python, sets are implemented using dictionaries, so this violates the O(1) memory requirement, and will also not pass the judge.

All of that said, it's pretty pathetic that whoever made this problem decided to go through these comments and downvote anyone who noted the obviously poor description of the problem, rather than providing an explanation for the problem's description / reasoning for why it is not unclear or misleading, as many people have noted.

Nothing is being "removed" from the array at any point, the duplicate elements are just being swapped with their following element until the initial array [1,2,2,2,3,3,3,4,4,4] is something akin to [1,2,3,4,3,3,3,4,4,4]. So nothing is ever necessarily removed, certain array elements are simply overwritten by the values of other array elements. Hence, the wording of the original question IS misleading and unclear, and the question could do with being renamed for the sake of clarity.

• @cjgali1 The problem is alright, people are just being sloppy/noobs. The problem says "Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively". Not the problem's fault if people ignore that and pretend it isn't there. And several people clearly tried to change the array by using something like `nums = ...`. Not the problem's fault if they don't know their language and don't realize that that doesn't do what they think it does.

It's also not an unreasonable function, it's almost the same as one of the C++ standard, namely unique, which also is for removing duplicates and also returns the new end point. It can even be used to trivially solve this problem:

``````class Solution {
public:
int removeDuplicates(vector<int>& nums) {
return unique(nums.begin(), nums.end()) - nums.begin();
}
};
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.