Remove Duplicates Leetcode Array Problem



Given an integer array nums sorted in non-descending order, it will be deduplicated so that each unique element appears exactly once. The relative order of the elements should remain the same.

Some languages ​​don't let you change the length of the array, so you need to put the result in the first part of the nums array instead. More formally, if there are k elements after removing duplicates, then the first k elements of num must contain the final result. It doesn't matter what you leave after the first k elements.

In the function, we have to return the n size of array ( first n elements remaining in the array).

We don't have to allocate another array of memory. You just have to modify that given array in place with O(1) extra memory.

As we know question statement is given an array and an integer. We have to remove all elements of the array which have the same value equal to Val and return the size of the array after operation in the function.

Custom Judge:

The compiler will check your solution with the following code:

int[] nums = [.....];// given array

int val = ...; // given value to be removed

int[] resultnums = [...]; // after the removal of the occurrence of Val , the final array returns as your answer



int n= removeEle(nums, val);  // calling function



assume n== resultnums.size();



sort(nums,0,n);  // Here, sorting of first n elements of nums

for(int i=0; i<oriinalLength(); i++) {

            assume nums[i] == resultnums[i];

}

Then, if your all test cases pass, your solution will be accepted.

 

Example 1:

Input: nums = [5,5,9]
Output: 2, nums = [5,9,_]
Explanation: Your solution must return n = 2, which are first 2 elements in array 5,9 respectively.
You don't have to worry what you left on duplicate elements, just return the array size in the end of function.

Example 2:

Input: nums = [3,3,4,4,4,5,5,6,6,7]
Output: 5, nums = [3,4,5,6,7,_,_,_,_,_]
Explanation: Your solution must return n = 5, which are first 5 elements in array 3,4,5,6,7 respectively.
You don't have to worry what you left on duplicate elements, just return the array size in the end of function.

 

Constraints:

  • 1 <= nums.size() <= 3 * 104
  • -100 <= nums[i] <= 100
  • nums is sorted in non-decreasing order.

𝐂++ 𝐒𝐨𝐥𝐮𝐭𝐢𝐨𝐧


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

Java Solution

class Solution{
    public
    int removeDuplicates(int[] nums){
        if(nums.length==0)
        return 0;
        int i=0;
        for(int j=1; j<nums.length; j++){
            if(nums[j]!=nums[i]){
                i++;
                nums[i]=nums[j];
            }
        }
        return i+1;
    }
}

Success Information
Runtime34 ms, faster than 17.07% of C++ leetcode submissions of Remove duplicates from sorted Array.
Memory Usage18.5 MB, less than 36.64% of C++ leetcode submissions of Remove duplicates from sorted Array.
Post a Comment (0)
Previous Post Next Post