Skip to content

Commit 8bb7a5a

Browse files
Merge pull request neetcode-gh#334 from abe-101/patch-1
Create 27-Remove-Element
2 parents 3023e5c + de244e4 commit 8bb7a5a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

java/27-Remove-Element.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public void swap(int[] a, int i, int j) {
3+
int temp = a[i];
4+
a[i] = a[j];
5+
a[j] = temp;
6+
}
7+
public int removeElement(int[] nums, int val) {
8+
int boundary = nums.length-1;
9+
int i = 0;
10+
while (i <= boundary) {
11+
if (nums[i] == val) {
12+
swap(nums, i, boundary);
13+
boundary -= 1;
14+
}
15+
else {
16+
i += 1;
17+
}
18+
}
19+
return i;
20+
}
21+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy