Step-by-Step Guide to Remove element Leetcode Problem no. 27

Remove all occurrences from array and return the length of final array

·

1 min read

Step-by-Step Guide to Remove element Leetcode Problem no. 27

Problem 27

Remove all occurrences from array and return the length of final array.

Intuition

We solve this problem by using replacing the indices of given array method

Try the code...

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