import java.io.*;
import java.util.*;

public class RemoveElement{
	public static int removeElement(int[] nums, int val){
		if(nums == null || nums.length < 1){
			throw new IllegalArgumentException();
		}

		int i = 0;
		int j = 1;
		int newLen = 0;

		while(j < nums.length){
			if(nums[i] == val){
				if(nums[j] != val){
					int n = nums[i];
					nums[i] = nums[j];
					nums[j] = n;
					i++;
					j++;
					newLen++;
				}else{
					j++;
					if(j == nums.length){
						break;
					}
				}
			}else{
				i++;
				j++;
				newLen++;
				if(j == nums.length){
					return (nums[i] == val) ? newLen : newLen + 1;
				}
			}
				
		}
		return newLen;
	}

	public static void main(String[] args){
		int[] nums = {1,3,2,4,1,1,6};
		try{
			System.out.println(removeElement(nums,1));
		}catch(IllegalArgumentException e){
			System.out.println(e.toString());
		}
	}
}


@@ -60,16 +60,37 @@
 lement%7B%0A
+%09//using one pointer%0A
 %09public 
@@ -113,16 +113,989 @@
 eElement
+3(int%5B%5D nums, int val)%7B%0A%09%09if(nums == null %7C%7C nums.length %3C 1)%7B%0A%09%09%09throw new IllegalArgumentException();%0A%09%09%7D%0A%0A%09%09int p = 0;%0A%09%09for(int i = 0;i %3C nums.length;i++)%7B%0A%09%09%09if(nums%5Bi%5D != val)%7B%0A%09%09%09%09nums%5Bp%5D = nums%5Bi%5D;%0A%09%09%09%09p++;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return p;%0A%09%7D%0A%09public static int removeElement(int%5B%5D nums, int val)%7B%0A%09%09if(nums == null %7C%7C nums.length %3C 1)%7B%0A%09%09%09throw new IllegalArgumentException();%0A%09%09%7D%0A%09%09int newLen = 0;%0A        %09int i = 0;%0A        %09int j = nums.length - 1;%0A%0A        %09while(i %3C= j)%7B%0A%09%09%09if(nums%5Bi%5D == val)%7B%0A                %09%09if(nums%5Bj%5D != val)%7B%0A                    %09%09%09int n = nums%5Bi%5D;%0A                    %09%09%09nums%5Bi%5D = nums%5Bj%5D;%0A              %09%09%09%09nums%5Bj%5D = n;%0A                    %09%09%09i++;%0A                    %09%09%09j--;%0A                    %09%09%09newLen++;%0A                %09%09%7Delse%7B%0A                    %09%09%09j--;%0A                %09%09%7D%0A            %09%09%7Delse%7B%0A                %09%09newLen++;%0A                %09%09i++;%0A            %09%09%7D%0A        %09%7D%0A%0A        %09return newLen;%0A%09%7D%0A%09%0A%09public static int removeElement2
 (int%5B%5D n
@@ -1726,16 +1726,16 @@
 %0A%09%09try%7B%0A
-
 %09%09%09Syste
@@ -1761,16 +1761,17 @@
 eElement
+3
 (nums,1)

add the solution by using one pointer.