8ms, >97% Java solution


  • 0
    A
    public class Solution {
        public int removeElement(int[] nums, int val) {
            int index = 0;
            int count_val = 0;
            int len = nums.length;
            for(int i = 0; i < nums.length; i++){
                if(nums[i] == val){
                    len--;
                }
                if(nums[i] != val){
                    nums[index] = nums[i];
                    index++;
                }
            }
            return len;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.