𝐑𝐞𝐦𝐨𝐯𝐞 𝐄𝐥𝐞𝐦𝐞𝐧𝐭𝐬 𝐋𝐞𝐞𝐭𝐜𝐨𝐝𝐞 𝐀𝐫𝐫𝐚𝐲 𝐏𝐫𝐨𝐛𝐥𝐞𝐦



Problem Statement of  Leetcode 

Here, we have given an array 'nums' and a value 'val', 

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 val = ...; // given value to be removed
int[] resultnums = [...]; // after the removal of occurence of val , final array return 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= {3,3,4,1,5,2}, val=3
Output: 4, nums={4,1,5,2}
Explanation: Function must return the value 4 left after
removing the all occurence of 'val' in the array

Example 2:

Input: nums= {0,2,3,4,3,5,6,7,3}, val=3
Output: 6, nums={0,2,4,5,6,7}
Explanation: Function must return the value
6 left after removing the all occurence of 'val' in the array

 

Constraints:

  • 0<=nums.size()<=100
  • 0<=nums[i]<=50
  • 0<=val<=100

Approach

This problem can be done using the following steps. Have a look at the following steps to understand the approach:-
1. We will have a function having vector 'nums' array and 'Val' to be removed as arguments.
2. What we will do is first find the size of array 'nums' and store it in a variable 'n'.
3. Now we will iterate each element in the array using for loop.
4. If any value is found equal to Val in iteration, we declare an if-statement in for loop for this. and just change that element to the previous position.
5. then, decrement the 'i' value by one and 'n' also.
6. In the last function will return the value n.


C++ Solution


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

𝐉𝐚𝐯𝐚 𝐒𝐨𝐥𝐮𝐭𝐢𝐨𝐧

class Solution {
    public
        int removeElement(int[] nums, int val) {
           if(nums.length==0)
               return 0;
            int n=nums.length;
            for(int i=0; i<n; i++){
                if(nums[i]==val){
                    nums[i]=nums[n-1];
                    i--;
                    n--;
                }
    }
            return n;
        }
    };

Success Information
Runtime3 ms, This code is faster than 69.71% of C++ which is submitted online on leetcode. 
Memory Used8.7 MB, less than 73.12% of C++ which is submitted online on leetcode. 

Post a Comment (0)
Previous Post Next Post