-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveElement.java
More file actions
34 lines (31 loc) · 879 Bytes
/
RemoveElement.java
File metadata and controls
34 lines (31 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class RemoveElement {
public static int removeElementHead(int[] nums, int val) {
int left = 0;
for (int right = 0; right < nums.length; right++) {
if (nums[right] != val) {
nums[left] = nums[right];
left++;
}
}
return left;
}
public static int removeElementHeadTail(int[] nums, int val) {
int left = 0;
int right = nums.length;
while (left < right) {
if (nums[left] == val) {
nums[left] = nums[right-1];
right--;
}else{
left++;
}
}
return left;
}
public static void main(String[] args) {
int[] nums = {0,1,2,2,3,0,4,2};
int val = 2;
int k = removeElementHeadTail(nums, val);
System.out.println(k);
}
}