-
Couldn't load subscription status.
- Fork 3
Remove a val in arr and return the length of remaining values #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| const { expect } = require("chai") | ||
| const removeElement = require("./solution"); | ||
|
|
||
| describe("remove element", () => { | ||
| it("should remove an element and return its length", () => { | ||
| const result = removeElement([3, 2, 1, 3], 3); | ||
| expect(result).to.equal(2); | ||
| }); | ||
|
|
||
| it("returns 4 when val is not in an arr below", () => { | ||
| const result = removeElement([3, 2, 1, 3], 5); | ||
| expect(result).to.equal(4); | ||
| }); | ||
|
|
||
| it("should return 0 when arr is empty", () => { | ||
| const result = removeElement([], 3); | ||
| expect(result).to.equal(0); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| -> Given an array arr and a value val, | ||
| -> remove all instances of that value in-place | ||
| -> and return the new length. | ||
|
|
||
| -> Do not allocate extra space for another array, | ||
| -> you must do this by modifying the input array in-place with O(1) extra memory. | ||
|
|
||
| -> The order of elements can be changed. | ||
| -> It doesn't matter what you leave beyond the new length. | ||
|
|
||
| Example:- | ||
|
|
||
| -> Given arr = [3,2,2,3], val = 3, | ||
| -> Your function should return length = 2 | ||
| -> with the first two elements of arr being 2. | ||
| -> It doesn't matter what you leave beyond the returned length. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| const removeElement = (arr, val) => { | ||
| // initialize count to zero | ||
| let counts = 0; | ||
| // loop through the arra | ||
| for (let i = 0; i < arr.length; i++) { | ||
| /** | ||
| * check if num is not equal to val | ||
| * then increment the number of counts | ||
| * but assigining its num values | ||
| */ | ||
| if (arr[i] != val) { | ||
| arr[counts++] = arr[i] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is not correct, take an example: arr = [1, 2, 4, 2, 5];
val = 2;
// when i = 0, arr[i] != val is true, so
// arr[counts++] = arr[i], basically arr[0] = arr[0], nothing has happened hereThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will check on this. |
||
| } | ||
| } | ||
| // return our counts | ||
| return counts; | ||
| } | ||
|
|
||
| exports = module.exports = removeElement; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should also add a test to show that the array is mutated; coz how about if I just count the elements and return that count?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The length should be different/less.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, feedback taken and will work on it.