4 lines Java Solution

  • 2

    Here is my simple java solution:

    public int removeElement(int[] nums, int val) {
            int count=0;
            for(int n:nums) if(n!=val) nums[count++]=n;
            if(count==0) nums=null; //If all elements are removed
            return count;

  • 0

    Nice solution!!

Log in to reply

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