38

This is leetcode 26. Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length. An example is given nums = [1,1,2], the function should return [1,2].

Below is my code. I delete all the other duplicates, just leave one of them. However I always got an error of reference binding to null pointer of type 'value_type' when submitting. I would appreciate if anyone can help me with this!

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int i = 0;
        while(i < nums.size() - 1) {
            if (nums[i] == nums[i + 1]) {
                nums.erase(nums.begin() + i);
            } 
            else i++;
        }
        return nums.size();
    }
};
3
  • Can't duplicate. ideone.com/ppuRg5. Commented Dec 22, 2017 at 22:25
  • Show a complete program that gives the error message. Commented Dec 22, 2017 at 22:29
  • @RSahu Good example of a lacking test suite ;) Commented Jan 28, 2019 at 15:20

6 Answers 6

46

vector<T>::size() returns a value of type size_t, which is an unsigned type. Let's say the vector passed in is empty and therefore the vector's length is 0. nums.size() - 1 will cause integer underflow and you will actually be comparing 0 with a very large positive number. This will evaluate to true causing the loop to run and i going pass the array bounds.

To fix this you can cast nums.size() to int preemptively or store the size in an integer variable and compare with that.

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

1 Comment

Or change it to while (i + 1 < nums.size()). This will work as long as nums.size() is not INT_MAX.
8

The function, as posted, works fine for a vector whose elements are [1 1 2]. See https://ideone.com/ppuRg5.

However, one problem I see in your function is that if you pass it an empty vector, it's going to run into problems.

while(i < nums.size() - 1)

will be a problem when nums is empty. You can preemptively avoid that problem by returning from the function immediately if you find that it is an empty vector.

Also, use an unsigned type for i to avoid compiler warnings about comparing signed and unsigned types.

int removeDuplicates(std::vector<int>& nums) {
   if ( nums.empty() )
   {
      return 0;
   }

   unsigned int i = 0;
   while(i < nums.size() - 1) {
      if (nums[i] == nums[i + 1]) {
         nums.erase(nums.begin() + i);
      } 
      else i++;
   }
   return nums.size();
}

Comments

2

This is not an answer to your question, but it would be more efficient solution to the problem if you didn't have to resize your vector each time you find a duplicate. Just to give you an idea, you could have two iterators i and j, i keeping the index of the last unique element of your solution vector and j iterating through the vector. When j points to a value not in the first i elements, copy that to v[i]. And once you are done, delete everything from the j-th place onwards.

Comments

0

In my case the reason of this error message was segmentation fault.

Example For empty input:

string longestCommonPrefix(vector<string>& strs) { 
    auto start = strs[0];

It works OK, when I add check for empty input

string longestCommonPrefix(vector<string>& strs) {
        if (strs.size() == 0) {
            return "";
        }
    auto start = strs[0];

Comments

0
class Solution {
  public:
    int removeDuplicates(vector < int > & nums) {
        for (int i = 1; i < nums.size(); i++) {
            if (nums[i] == nums[i - 1]) {
                nums.erase(nums.begin() + i);
                i--;
            }
        }
        return nums.size();
    }
};

Comments

-2

I just figured out that instead of comparing or operating num.size() with any integer directly First store int n=num.size(); and then compare it like if(num=n-2).

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.