How should I improve this?
    public int removeElement(int[] nums, int val) {
        int length = nums.length;
        for(int i = 0; i < length; i++){
            if(nums[i] == val){
                for (int j = i+1; j < length; j++) {
                    nums[j - 1] = nums[j];
                }
                i--;
                length--;
            }
        }
        return length;
    }
}

So this is like a jank solution for the deletion of a value throughout the array. My question is that I did i-- because I kept making i the duplicate value(when they are next to each other). This was accepted, but is there a way I can make this better?

Comments (1)