diff --git a/java/27-Remove-Element.java b/java/27-Remove-Element.java new file mode 100644 index 000000000..1a3853407 --- /dev/null +++ b/java/27-Remove-Element.java @@ -0,0 +1,21 @@ +class Solution { + public void swap(int[] a, int i, int j) { + int temp = a[i]; + a[i] = a[j]; + a[j] = temp; + } + public int removeElement(int[] nums, int val) { + int boundary = nums.length-1; + int i = 0; + while (i <= boundary) { + if (nums[i] == val) { + swap(nums, i, boundary); + boundary -= 1; + } + else { + i += 1; + } + } + return i; + } +}
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: