0ms and clean Java solution


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

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

Log in to reply
 

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