Remove Duplicates from Sorted Array Problem & Solution

Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.

See the remove duplicates from sorted array problem on LeetCode.

C++ Solution

#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")

static const int _=[](){std::ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

class Solution {
public:
  int removeDuplicates(vector<int>& nums) {
    if (nums.size() == 0) {
      return 0;
    }

    int j = 0;

    for (int i = 1; i < nums.size(); ++i) {
      if (nums[j] != nums[i]) {
        nums[++j] = nums[i];
      }
    }

    return j + 1;
  }
};

Are you looking for a job?