Skip to content

Commit 3f0c0fd

Browse files
committed
feat: add remove element solution
1 parent 4bcecd3 commit 3f0c0fd

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

leetcode/remove-element/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Remove Element
2+
3+
## Description

leetcode/remove-element/solution.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const removeElement = (nums, val) => {
2+
let i = 0;
3+
for (let j = 0; j < nums.length; j++) {
4+
if (nums[j] !== val) {
5+
nums[i] = nums[j];
6+
i++;
7+
}
8+
}
9+
return i;
10+
};
11+
12+
module.exports = removeElement;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const removeElement = require('./solution');
2+
3+
describe('Remove Element', () => {
4+
const testCases = [
5+
{
6+
input: [
7+
[3, 2, 2, 3],
8+
3,
9+
],
10+
output: 2,
11+
},
12+
{
13+
input: [
14+
[0, 1, 2, 2, 3, 0, 4, 2],
15+
2,
16+
],
17+
output: 5,
18+
},
19+
];
20+
21+
it('should return a number type', () => {
22+
expect(typeof removeElement([1, 1, 2])).toBe('number');
23+
});
24+
25+
it.each(testCases)('should return $output', (testCase) => {
26+
expect(removeElement(...testCase.input)).toBe(testCase.output);
27+
});
28+
});

0 commit comments

Comments
 (0)