How to Remove Element From An Array – Leetcode Solution # 27

  • Post last modified:December 15, 2022
  • Reading time:4 mins read

Introduction

In this tutorial , we will solve leetcode problem # 27 . This problem is good for understanding basics of Array manipulation. We will solve it using Java programming Language.

Problem Statement

As per problem, we have been given an array nums and an integer value val , our goal is to remove all the occurrence of val from the array and while doing so we can change the relative order of elements in nums.
Given array should be modified such that if K is # of remaining elements after removing all the elements with value val, then the nums first k element should consist result and it doesn’t matter what is in the array after that. ( This is due to the fact that we cannot change the length of array in some languages )

Lets see some example for better understanding

nums = [ 3,2,2,3 ] ;
val = 3;
-> removing val=3 from nums array = [_,2,2,_]
-> but since we need to arrange remaining elements , we should push 2,2 to the front of the array.
-> that means our result array should be = [2,2,_,_]

Thinking About Solution

First things that comes to mind is that we should passthrough the nums array and count the all the occurrence of val in the nums array, plus whichever place we find val in the nums array, we understand that we have to write our logic that push that element towards end or somehow remove from the array.

One of the logic that comes to the mind is that we should keep an index such as j=0; which we only use for the values which are not = val. That means during our passthrough of nums array if current_element ! = val , then and only then we assign nums[j] = current_element and increment the j value for the next element.

So its like j index is used for finalizing position for each element in the result array. Lets see the code for more clarity

Solution

  • We first assigned count variable =0; this will be useful for returning the length of the result array.
  • We also assigned index variable j=0; this index is used for the result array position for the cases element !=val
  • Lets traverse nums array with for loop ;
    • if current_element == val, then we just increment the count and skip to next element.
    • if current_element !=val then we know this will be part of our final result so lets add it to the our result array.
  • In case of nums = [3,2,2,3] , val =3;
    • For the first pass , i=0;j=0; count = 1; nums = [3,2,2,3]. Nothing changed in array because we are not sure which element to put in first index
    • For the second pass, i=1;j=1;count=1; nums = [2,2,2,3]. In this pass, current_element !=val , hence j=0 position can be fixed and it will be current_element i.e nums[1]. since j=0 is fixed we will increment counter to fix the next element.
    • For the third pass, i=2;j=2; count=1 nums = [2,2,2,3]. In this pass again current_element != val , hence j=1 position can be fixed and it will be current_element i.e nums[2]. now j=1 is also fixed so we will increment it to j=2.
    • For the fourth pass i=3;j=2; count=2 nums=[2,2,2,3]. In this pass current_val = val, hence count will be incremented and loop will move to next pass which is actually end of the loop.
  • So after loop finishes , we are left with count = 2; nums[2,2,2,3] , out result length would be total length of the array – counted val , hence our return value would be 4-2 = 2;
  • And result would be first 2 elements of the nums i.e [2,2] and as said in the problem other elements will be ignored , so remaining elements [2,3] will be ignored
import java.util.*;
class Solution {
    public int removeElement(int[] nums, int val) {
        
        int count = 0;
        int j=0;
        for(int i=0;i<nums.length;i++){
            if(nums[i]==val){
                count=count+1;
                continue;
            }
            nums[j]=nums[i];
            j++;
        }
        
        return nums.length-count;
        
    }
}

Result

If we run above code it will pass the all the test cases with runtime almost 0 ms. Since we are using one for loop time complexity is O(N), space complexity is constant since we are not using any additional space except some constant to track value.

Conclusion

In this article we go through the remove element problem from Leetcode. This problem is good exercise for beginners to learn about array manipulation.

Bonus Tip

  • If you want to upskill your coding interview game, you can definitely check out this bestseller course ( this is in Java )

Leave a Reply