-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_27_remove_element.java
More file actions
35 lines (31 loc) · 863 Bytes
/
Copy path_27_remove_element.java
File metadata and controls
35 lines (31 loc) · 863 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
35
public class _27_remove_element {
public static int removeElement(int[] a, int val) {
int n = a.length;
for (int i = 0; i < n; i++) {
if (a[i] == val) {
for (int j = i; j < n-1; j++) {
a[j] = a[j+1];
}
n--;
i--;
}
}
return n;
}
public static int removeElement_2pointers(int[] a, int val) {
int k = 0;
for (int i = 0; i < a.length; i++) {
if(a[i]!= val) {
a[k]=a[i];
k++;
}
}
return k;
}
public static void main(String[] args) {
int[] a = {1,2,3,4};
System.out.println(removeElement(a, 3));
// System.out.println(removeElement_2pointers(a, 3));
System.out.println("DONE");
}
}