Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8509ee0

Browse files
Remove Element from array Solved
1 parent 7be34d3 commit 8509ee0

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ npm test
1010

1111
## Array Problems Solution -
1212
1. [Two Sum](https://leetcode.com/problems/two-sum/) - [Solution](https://github.com/ManiruzzamanAkash/LeetCode-In-JS/blob/main/two-sum/)
13-
1. [Remove duplicates from sorted array]() - [Solution](https://github.com/ManiruzzamanAkash/LeetCode-In-JS/tree/main/remove-duplicates-from-sorted-array)
13+
1. [Remove duplicates from sorted array]() - [Solution](https://github.com/ManiruzzamanAkash/LeetCode-In-JS/tree/main/remove-duplicates-from-sorted-array)
14+
1. [Remove elements from array]() - [Solution](https://github.com/ManiruzzamanAkash/LeetCode-In-JS/tree/main/remove-element)

‎remove-duplicates-from-sorted-array/index.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ test("[0,0,1,1,1,2,2,3,3,4] array would result 5", () => {
1111
// Test with array
1212
const nums = [1, 1, 2, 4, 4, 5]; // Input array
1313
let expectedNums = [1, 2, 4, 5, 4, 5]; // The expected answer with correct length
14-
const expectTotalFound = 4;
1514

1615
test("nums = [1, 1, 2, 4, 4, 5] array would result 4 with array [1, 2, 4, 5, 4, 5]", () => {
1716
expect(removeDuplicates(nums)).toStrictEqual(4);

‎remove-element/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Remove an element from the array.
3+
*
4+
* @param {number[]} nums
5+
*
6+
* @return {number}
7+
*/
8+
var removeElement = function(nums, val) {
9+
let i = 0;
10+
while (i < nums.length) {
11+
if (nums[i] === val) {
12+
nums.splice(i, 1);
13+
} else {
14+
++i;
15+
}
16+
}
17+
18+
return nums.length;
19+
};
20+
21+
module.exports = removeElement;

‎remove-element/index.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const removeElement = require("./index");
2+
3+
test("[3,2,2,3] array and val=3 would result 2", () => {
4+
expect(removeElement([2, 2], 3)).toStrictEqual(2);
5+
});
6+
7+
test("[0,1,2,2,3,0,4,2] array and val=2 would result 5", () => {
8+
expect(removeElement([0, 1, 2, 2, 3, 0, 4, 2], 2)).toStrictEqual(5);
9+
});
10+
11+
// Test with array
12+
let nums = [0, 1, 2, 2, 3, 0, 4, 2]; // Input array
13+
const val = 2;
14+
let expectedNums = [0, 1, 3, 0, 4]; // The expected answer with correct length
15+
16+
test("nums = [0, 1, 2, 2, 3, 0, 4, 2] array would result 5 with array [0, 1, 3, 0, 4]", () => {
17+
expect(removeElement(nums, val)).toStrictEqual(5);
18+
19+
// Test also the previous array
20+
expect(nums).toStrictEqual(expectedNums);
21+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /